Sort an array of objects based on one numerical object property in JavaScript? In descending order.

nick asked this question 21 days ago
nick 21 days ago · Edited

I have an array of objects that looks similar to the following:

  {id: "1", name: "nick", points: 12},
  {id: "2", name: "bob", points: 64},
  {id: "3", name: "ricky", points: 15},
  {id: "4", name: "nikky", points: 51},

  . . .


How do I sort this array based on the points property value from largest to smallest (descending)?

This is all done in JavaScript.

1 suggested answers
yaboy01 13 days ago · Edited

This will sort() your array of objects in descending order based on the points property.

ES5 version:

array.sort(function(a, b) {
  return parseFloat(b.points) - parseFloat(a.points)

ES6 version:

array.sort((a, b) => b.points - a.points)
0 replies