1

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...

1
  • You can get the rate associated with an id in your sort callback using recommendations[id].rate Commented Jan 24, 2018 at 10:00

3 Answers 3

5

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);

Sign up to request clarification or add additional context in comments.

Comments

2

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);

3 Comments

Note that the sort callback should return 0 or a value smaller or greater than 0, not true or false.
Isn't that true==1 is true and false==0 is true.
And when will it return anything < 0?
1

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

2 Comments

a.rate and b.rate will always be undefined.
@deceze thanks My bad, on this one, had an hard time on posting with my phone, guess it's always better to test :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.