2

Is this the correct way to sort an array of objects?:

class Alarm{
    constructor(data){
        this.a = data.a;
        this.b = data.b;
    }
}

const a1 = new Alarm({a:4,b:1});
const a2 = new Alarm({a:1,b:4});
const a3 = new Alarm({a:2,b:3});
const a4 = new Alarm({a:3,b:2});

let a = [a1,a2,a3,a4];

a.sort((x,y)=>x.a>y.a);

for(const i of a){
    console.log(i)
}

a.sort((x,y)=>x.b>y.b);

for(const i of a){
    console.log(i)
}

When I look at the documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

They are doing this instead, which seems extremely awkward:

a.sort((x,y)=>
    {
        if(x.a>y.a) return 1;
        if(x.a<y.a) return -1;
        return 0;
    });
11
  • you will want x.a - y.a Commented Nov 2, 2016 at 9:02
  • sort should return 3 values. 1 for greater than, 0 for no change and -1 for less than. Commented Nov 2, 2016 at 9:03
  • @Keith for string values, I'm not sure how - would work. For numeric values, it will work, but as OP is asking for better approach, I guess we should consider both situations Commented Nov 2, 2016 at 9:06
  • @Rajesh Same as < or >, it would be coerced to a number Commented Nov 2, 2016 at 9:07
  • It's not awkward. It's how the comparator function works. By the way, what is your question? Commented Nov 2, 2016 at 9:14

1 Answer 1

5

You could use the difference for sorting

a.sort((x, y) => x.a - y.a);

Taken from Array#sort

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
  • compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

class Alarm{
    constructor(data){
        this.a = data.a;
        this.b = data.b;
    }
}

const a1 = new Alarm({a:4,b:1});
const a2 = new Alarm({a:1,b:4});
const a3 = new Alarm({a:2,b:3});
const a4 = new Alarm({a:3,b:2});

let a = [a1, a2, a3, a4];

a.sort((x,y) => x.a - y.a);

console.log(JSON.stringify(a, 0, 4));

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

5 Comments

Is there a logical reason as to why the sort lambda should return 1, -1, and 0 instead of a boolean value. Seams really weird to me. Is there an alternative way of sorting where you can return booleans? I'm new to JS and ES6 :)
@Baz Because that's how it works. What would a boolean mean? The return value needs to indicate whether the first parameter comes before or after the second, or at the same location.
The sort needs to know if a value is equal, greater than, or less than. A boolean does not give you that.
To understand why <=> are needed, a good idea is to look at how sort functions are done. Look for bubble sort / quick sort etc. Wiki most likely will have good docs on this.
great! the spaceship operator :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.