1

I have an array of objects with two keys:

  1. Score
  2. Date

I need to sort the array in reverse order, from highest to lowest. I have this code to sort the array by score:

highscores.sort(function(a, b) {
  return b.score - a.score;
});

I'd like to sort it by date also if the score values are equals, i.e.

Array = [
  {score: 200, date: 11/11/2016}, 
  {score: 300, date: 11/11/2016}, 
  {score: 200, date: 12/11/2016}
];

I expect this result:

Array = [
  {score: 300, date: 11/11/2016},
  {score: 200, date: 12/11/2016},
  {score: 200, date: 11/11/2016}       
];
9
  • 1
    This answer may provide a simpler, more to-the-point solution Commented Nov 22, 2016 at 2:47
  • You can check my answer below which is simply using getTime method Commented Nov 22, 2016 at 2:50
  • @RuslanOsmanov I checked that answer but was so complicated to implement. Thank you anyway :) Commented Nov 22, 2016 at 2:54
  • @qxz Thanks for the link. :) Commented Nov 22, 2016 at 2:55
  • 1
    @Aruna Yep, that was what I was looking for. It's perfect. Just need a and b variables in the date part. d.date - a.date and runs perfect. Thank you. :) Commented Nov 22, 2016 at 2:57

2 Answers 2

3

Here you go,

You can further sort by date if the difference between the score equals 0.

You can simply subtract the date as below.

var highscores = [
  {score: 200, date: '11/11/2016'}, 
  {score: 300, date: '11/11/2016'}, 
  {score: 200, date: '12/11/2016'}
];

highscores.sort(function(a, b) {
  var c = b.score - a.score;
  return c === 0 ? new Date(b.date).getTime() - new Date(a.date).getTime() : c; // Even you can use without 'getTime' since the '-' operator implicitly do this
});

console.log(highscores);

Note: Date conversion inside the sort method is to make the sample to work. Instead actual object array should have the date object

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

10 Comments

you have to change your data compare, in down order.
@KrisRoofe Thanks and I have corrected this.
Note that you don't need the .getTime(), because the - operator implicitly converts its operands to numbers first.
How can I delete the [duplicate] tag from my post, I saw the answer of that question and is not the same. It's a horrible implementation that answer. I could do that by myself for solve this but I wanted this short one.
@Aruna Yep, unfortunately we're more bad programmers than good ones. XD But someday I'd help when I learn more.
|
0

Keep dates in string format for conversion, Example snippet

var arr = [{
  score: 200,
  date: "11/11/2016"
}, {
  score: 300,
  date: "11/11/2016"
}, {
  score: 200,
  date: "12/11/2016"
}];

var c = arr.sort(predictsort());
console.log(c);

function predictsort() {
  sorter = function(c, d, key) {
    return key === "score" ? d[key] - c[key] : new Date(d[key]) - new Date(c[key]);
  }
  return function(a, b) {
    return sorter(a, b, "score") === 0 ? sorter(a, b, "date") : sorter(a, b, "score")
  }
}

2 Comments

Thanks buddy for the help. ;)
no problem at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.