Q:

sort objects in array with dynamic keys

I am trying to sort an array of dynamic key / values (Object) based on a property in the object. Can someone please provide me an example of how to do this. Ill try to create a similar structure to my issue
Order Class:
export class order {
  id: number; 
  productRank: number; 
  productId: number; 
  productName: string;
}

There is a storeOrders observable array of orders in my component
storeOrders$: Observable<Array<orders>>;

The key in the orders array is dynamically generated and is stored with the key/value containing  as key and order object as value
then the orders object for example is something like: 
 let orders = {
12_123: { id: 123, productRank: 3, productId: 23, productName: 'shirt'},
23_124: { id: 124, productRank: 1, productId: 14, productName: 'cologne'},
67_124: { id: 125, productRank: 2, productId: 45, productName: 'belt' }       
 }

When i subscribe to this data, how can I iterate over the storeOrders array and sort items based on the productRank. I have been trying to get this right but was not able to get this working, can someone please point out how to iterate over dynamic keys in an array and do a sort based on a property in the value object? Thank you, your helps really appreciated!   

A:

Your orders should be a object as below,
orders = {
    12_123: { id: 123, productRank: 3, productId: 23, productName: 'shirt' },
    23_124: { id: 124, productRank: 1, productId: 14, productName: 'cologne' },
    67_124: { id: 125, productRank: 2, productId: 45, productName: 'belt' }
  };

you can form the values by using Object.values(orders)
I use sortBy from lodash library as I'm used to it and you get the sorted values as
console.log(sortBy(Object.values(orders),['productRank']));

Stackblitz

