I have the following object:
let res = {
'name': {
age: 10,
density: 33,
length: 2
},
'greet': {
age: 33,
density: 92,
length: 3
},
'gyrt': {
age: 91,
density: 2,
length: 47
},
.
.
.
}
Which has much more key value pairs. I am asked to return the list of keys in the descending order of "ranking" which is governed by the below constraints:
- Greater age must be ranked higher
- Words with low density must be ranked higher
- Words with high length must be ranked higher
I am confused how to achieve this. I am trying to sort object but that does not help. I first tried to sort with age
, then with density
, and then with length
but it clearly does not work because object is re-ordered again and again while forgetting the previous order. How could I achieve this?
This is what I was trying:
let sortByAge = keys.sort((a, b) => Number(res[b].age) - Number(res[a].age));
let sortByDensity = keys.sort((a, b) => Number(res[b].density) - Number(res[a].density));
And same with length
. I cannot understand how to combine all the factors and order them.