Laurent Maquet
Laurent Maquet

Reputation: 400

Sort dictionary's keys by nested property

I have this kind of dictionary:

const recommendations =
 { '014A8C3D-FE99-4DE3-9F9F-F197A3CB270F': 
   { zL: 0.0402632597754549,
     zD: 0,
     mL: 2,
     mD: 0,
     rate: 0.02013162988772745 },
  '036EA860-FDF1-40DA-8915-A2F02241DC7D': 
   { zL: 0.0297356539387,
     zD: 0,
     mL: 1,
     mD: 0,
     rate: 0.0297356539387 },
  '4A49B2C5-62A6-494B-BE77-FBEF5C102579': 
   { zL: 0.052390243902439025,
     zD: 0,
     mL: 1,
     mD: 0,
     rate: 0.052390243902439025 },
  '640674E4-5080-4BFD-983F-330D1C9150D0': 
   { zL: 0.011376894745,
     zD: 0,
     mL: 1,
     mD: 0,
     rate: 0.011376894745 }
 }

Each key is a "recommendation identifier", and each value is a dictionary of recommendation's properties.

I would like to get an array of recommendation Ids, sorted by the 'rate' property (descending order):

//sortedRecommendations
['4A49B2C5-62A6-494B-BE77-FBEF5C102579',
'036EA860-FDF1-40DA-8915-A2F02241DC7D',
'014A8C3D-FE99-4DE3-9F9F-F197A3CB270F',
'640674E4-5080-4BFD-983F-330D1C9150D0']

I guess I should first transform my dictionary (not sortable) into an array (sortable). But then I couldn't find the way to get the expected result...

Upvotes: 1

Views: 218

Answers (3)

Fabien Greard
Fabien Greard

Reputation: 1894

Using Object.keys() and Array.sort()

Object.keys(recommendations).sort((a, b) => ( recommendations[a].rate < recommendations[b].rate ? -1 : 1)); //asc

Object.keys(recommendations).sort((a, b) => ( recommendations[a].rate < recommendations[b].rate ? 1 : -1)); //desc

edit: a.rate is indeed undefined. recommendations[a].rate

Upvotes: 1

Vignesh Raja
Vignesh Raja

Reputation: 8761

This works.

const recommendations =
{ '014A8C3D-FE99-4DE3-9F9F-F197A3CB270F': 
 { zL: 0.0402632597754549,
   zD: 0,
   mL: 2,
   mD: 0,
   rate: 0.02013162988772745 },
'036EA860-FDF1-40DA-8915-A2F02241DC7D': 
 { zL: 0.0297356539387,
   zD: 0,
   mL: 1,
   mD: 0,
   rate: 0.0297356539387 },
'4A49B2C5-62A6-494B-BE77-FBEF5C102579': 
 { zL: 0.052390243902439025,
   zD: 0,
   mL: 1,
   mD: 0,
   rate: 0.052390243902439025 },
'640674E4-5080-4BFD-983F-330D1C9150D0': 
 { zL: 0.011376894745,
   zD: 0,
   mL: 1,
   mD: 0,
   rate: 0.011376894745 }
};
 
var recommendationkeys = Object.keys(recommendations);

recommendationkeys.sort(function(first,second){
    return recommendations[second].rate - recommendations[first].rate;
});

console.log(recommendationkeys);

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386654

You could sort by rate property descending by getting the delta of the compairing properties.

var recommendations = { '014A8C3D-FE99-4DE3-9F9F-F197A3CB270F': { zL: 0.0402632597754549, zD: 0, mL: 2, mD: 0, rate: 0.02013162988772745 }, '036EA860-FDF1-40DA-8915-A2F02241DC7D': { zL: 0.0297356539387, zD: 0, mL: 1, mD: 0, rate: 0.0297356539387 }, '4A49B2C5-62A6-494B-BE77-FBEF5C102579': { zL: 0.052390243902439025, zD: 0, mL: 1, mD: 0, rate: 0.052390243902439025 }, '640674E4-5080-4BFD-983F-330D1C9150D0': { zL: 0.011376894745, zD: 0, mL: 1, mD: 0, rate: 0.011376894745 } },
    keys = Object
        .keys(recommendations)
        .sort((a, b) => recommendations[b].rate - recommendations[a].rate);

console.log(keys);

Upvotes: 5

Related Questions