user1037355
user1037355

Reputation:

sorting js objects

I have an object which looks like this:

[
Object {url="123456", name="john", count="0", url="name/sine"},
Object {url="1668", name="ben", count="0", url="name/dcdc"},
Object {url="98465", name="mike", count="0", url="name/ssd"},
]

I need to sort the object by name but can't figure it out.

I found the following snippet which sort object with 2 parts:

namesObject.sort(function(a, b){
    if(a.value > b.value){
        return 1;
    }
    else if(a.value < b.value){
        return -1;
    }
    return 0;
});

But how could I expand this to an object with four parts?

Upvotes: 0

Views: 139

Answers (1)

alex
alex

Reputation: 490557

You can do it easily like so...

arr.sort(function(a, b) { return a.name.localeCompare(b.name); });

Upvotes: 2

Related Questions