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}       
];
Jongware
  • 22,200
  • 8
  • 54
  • 100
andresscode
  • 1,415
  • 1
  • 10
  • 23
  • 1
    [This answer](http://stackoverflow.com/a/21980877/1848578) may provide a simpler, more to-the-point solution – qxz Nov 22 '16 at 02:47
  • You can check my answer below which is simply using `getTime` method – Aruna Nov 22 '16 at 02:50
  • @RuslanOsmanov I checked that answer but was so complicated to implement. Thank you anyway :) – andresscode Nov 22 '16 at 02:54
  • @qxz Thanks for the link. :) – andresscode Nov 22 '16 at 02:55
  • @lbpeppers Check my answer below which is very simple and elegant :-) – Aruna Nov 22 '16 at 02:56
  • 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. :) – andresscode Nov 22 '16 at 02:57
  • Please do not add "Solved" to your title, that is not how Stack Overflow works. If a question is suggested to be a duplicate, do not merely state it isn't one (especially not in ALL CAPS). [Edit] your question to indicate how it's different. – Jongware Nov 22 '16 at 10:50
  • @Rad Lexus But I need to write how's different inside the question text or in the field which asks me for a brief summary of why I edited my question? Sorry, I'm new here and I don't know exactly how this website works. And how can I delete the duplicate tag? Because that answer is different to this one. – andresscode Nov 22 '16 at 23:09
  • Reading your "I'd like to sort it by date also if the score values are equals", it sure looks to me this is the same question. You got the reverse-sort working, so what's left is sorting on the other field. Hence the pointer to the other question. – Jongware Nov 22 '16 at 23:13

2 Answers2

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

Aruna
  • 11,959
  • 3
  • 28
  • 42
  • you have to change your data compare, in down order. – LF00 Nov 22 '16 at 02:55
  • @KrisRoofe Thanks and I have corrected this. – Aruna Nov 22 '16 at 02:57
  • 1
    Note that you don't need the `.getTime()`, because the `-` operator implicitly converts its operands to numbers first. – qxz Nov 22 '16 at 02:59
  • Didn't know i was in a race (=. also small optimization quibble but you probably want to instantiate all your "Date" objects in place on the array. It might have a performance impact if you throw a really large array at it. – Adrian Nov 22 '16 at 03:01
  • Yep, don't worry guys the date format here is not relevant. I was needing just the general concept of sorting mulitple keys. Thanks. – andresscode Nov 22 '16 at 03:04
  • 1
    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. – andresscode Nov 22 '16 at 03:05
  • @lbpeppers Yes you are correct, all I saw in StackOverflow is horrible impementation :-) – Aruna Nov 22 '16 at 03:07
  • @qxz Thanks for the explanation – Aruna Nov 22 '16 at 03:07
  • 1
    @Aruna Yep, unfortunately we're more bad programmers than good ones. XD But someday I'd help when I learn more. – andresscode Nov 22 '16 at 03:10
  • @lbpeppers yes ofcourse – Aruna Nov 22 '16 at 03:13
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")
  }
}
Ajay Narain Mathur
  • 5,326
  • 2
  • 20
  • 32