I have an array of objects with the following type:
{
foo: number;
bar: number;
rankFoo?: number;
rankBar?: number;
}
The naive approach would be to sort the objects by each property, then loop over the array and rank each object by index:
// sort by foo
arr.sort((a, b) => a.foo > b.foo ? -1 : a.foo === b.foo ? 0 ? 1)
// rank by foo
for(let i = 0; i < arr.length; i++){
arr[i].rankFoo = i;
}
// repeat the same process for bar...
I'm looking for an algorithm with less complexity than this approach.