0

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?

1 Answer 1

2

You can do it easily like so...

arr.sort(function(a, b) { return a.name.localeCompare(b.name); });
Sign up to request clarification or add additional context in comments.

1 Comment

That was easy. Thank you! I didn't know about localeCompare... just the ticket!

Your Answer

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