0

I have an array like following

[{
  "score": 12,
  "time": 97192
},
{
  "score": 3,
  "time": 144391
},
{
  "score": 15,
  "time": 900039
},
{
  "score": 3,
  "time": 143962
}]

I want to have the result like the following

[
{
  "score": 15,
  "time": 900039
},
{
  "score": 12,
  "time": 97192
},
{
  "score": 3,
  "time": 143962
},
{
  "score": 3,
  "time": 144391 
}]

The score should be sorted in descending order while the time for the same score should be sorted in ascending order.

  newScoreArray.sort(function(a, b) {
    if(a.score === b.score) {
      var x = a.timeTaken,
        y = b.timeTaken;
        if(x>y){
          return -1;
        }else{
          return 1;
        }
    }
    return b.score - a.score;
  });

But this is just sorting according to the score only. Please suggest me what I am doing wrong here. Also if you have a better idea then please help me with it.

dangchithao
  • 613
  • 2
  • 8
  • 24
Mars Moon
  • 94
  • 2
  • 14
  • 1
    `newScoreArray.sort((a, b) => a.score - b.score || a.time - b.time)` or `newScoreArray.sort(({score: scoreA, time: timeA}, {score: scoreB, time: timeB}) => scoreA - scoreB || timeA - timeB)` would be the idiomatic way. `a - b` for numbers, `a.localeCompare(b)` for strings, chained with `||`. – Sebastian Simon Aug 08 '18 at 04:23
  • Possible duplicate of [Sort by two values prioritizing on one of them](https://stackoverflow.com/questions/4576714/sort-by-two-values-prioritizing-on-one-of-them) – Mark Aug 08 '18 at 04:25
  • Possible duplicate of [Javascript - Sort Array of objects by 2 Properties](https://stackoverflow.com/questions/45741397/javascript-sort-array-of-objects-by-2-properties) – Sebastian Simon Aug 08 '18 at 04:25
  • @MarkMeyer Nope its slightly different from that, I already tried that solution before posting this question. – Mars Moon Aug 08 '18 at 04:35
  • @Xufox Nope its slightly different from that, I already tried that solution too before posting this question. – Mars Moon Aug 08 '18 at 04:35
  • If you want to change the order, just swap the operands: instead of `a - b` use `b - a`. – Sebastian Simon Aug 08 '18 at 04:37
  • @Xufox I have tried it. Does not work. – Mars Moon Aug 08 '18 at 04:41
  • 1
    @MarsMoon: This answer is from that question and delivers your desired results : `data.sort(function (a, b) { return b.score - a.score || a.time - b.time; });` – Mark Aug 08 '18 at 04:43

2 Answers2

1

This is rather very easy

let a = [{
  "score": 12,
  "time": 97192
},
{
  "score": 3,
  "time": 144391
},
{
  "score": 15,
  "time": 900039
},
{
  "score": 3,
  "time": 143962
}]

// for ascending sort
a.sort((a,b) => {
  if (a.score == b.score) return a.time - b.time;
  return a.score - b.score;
})

// for your case 
a.sort((a,b) => {
  if (a.score == b.score) return a.time - b.time;
  return b.score - a.score;
})


console.log(a)
Prasanna
  • 4,125
  • 18
  • 41
  • Thank you for your answer. But unfortunately its not working on my case. I have updated the question, please review it if possible. – Mars Moon Aug 08 '18 at 04:33
0

Or if you want to be really terse:

let a = [{
  "score": 12,
  "time": 97192
},
{
  "score": 3,
  "time": 144391
},
{
  "score": 15,
  "time": 900039
},
{
  "score": 3,
  "time": 143962
}]

a.sort((a,b) => b.score - a.score || a.time - b.time)

console.log(a)

If scores are equal, b.score - a.score evaluates to 0 (false) so the times will be used.

Timshel
  • 1,653
  • 12
  • 9