1

I am trying to sort object array according to the points. In response, I am getting as shown in below image.

enter image description here

I want it to start from highest points_earned to lowest. For now it start with 50 and end with 1000. I want it to be start from 1000 and end with 50.

I have tried with the following code but doesn't seem working. Please help.

var homes = response.response.conversion_summary_stats;
console.log(homes);
homes.sort(function(a,b) { return a.points_earned.valueOf() < b.points_earned.valueOf();});
console.log(homes);
Santosh
  • 3,477
  • 5
  • 37
  • 75

1 Answers1

1

Subtract the values:

This approach sorts in ascending direction

homes.sort(function(a,b) { return a.points_earned - b.points_earned;});

This approach sorts in descending direction

homes.sort(function(a,b) { return b.points_earned - a.points_earned;});
Ele
  • 33,468
  • 7
  • 37
  • 75