When using the Angular keyvalue pipe to iterate over an object\'s properties as follows:
This is same as accepted answer, but it has more complex object so It can help somebody How to sort by custom index field and using keyval pipe.
In angular component:
myObject = {
"key1": { val:"whateverVal1", code:"whateverCode1", index: 1},
"key2": { val:"whateverVal2", code:"whateverCode2", index: 0},
"key3": { val:"whateverVal3", code:"whateverCode3", index: 2}
}
Sorting function in component:
indexOrderAsc = (akv: KeyValue, bkv: KeyValue): number => {
const a = akv.value.index;
const b = bkv.value.index;
return a > b ? 1 : (b > a ? -1 : 0);
};
in template:
...