Sorting objects depending on their property value

 
 array.sort(function (a, b) {
    if (a.points > b.points) {
        return -1;
    }
    if (a.points < b.points) {
      return 1;
    }
    // a must be equal to b
    return 0;
  });

For reusable use I would use this:

array.sort(function (a, b) {
      var property = 'year';
       if (a[property] > b[property]) {
           return -1;
       }
       if (a[property] < b[property]) {
         return 1;
       }
       // a must be equal to b
       return 0;
     });

Add your comment