2

How easy/hard is it to order collections in javascript (alphabetical and numerically).

Say I have a collection like:

var map = {

    user: { id: "23434", username: "mrblah" },
    user: { id: "1010", username: "johnskeet" }

};

And I want to order the collection by id and username.

Update correction thanks:

var map = [ { id: "23434", username: "mrblah" }, { id: "1010", username: "johnskeet" } ];
4
  • 3
    You can't have two members named user in your object. Perhaps what you want is an array? var map = [ { id: "23434", username: "mrblah" }, { id: "1010", username: "johnskeet" } ]; Commented Nov 5, 2009 at 2:20
  • you need to convert JSON object to array then its peace of cake Commented Nov 5, 2009 at 2:30
  • 3
    Let's not confuse JavaScript object literals with JSON. Commented Nov 5, 2009 at 2:53
  • @Ates Goral: Whats difference between the two. I could only see it as JSON object of the form json.org/object.gif Commented Nov 5, 2009 at 3:18

2 Answers 2

3
var map = { 
    users: [
        { id: "23434", username: "mrblah" },
        { id: "1010", username: "johnskeet" }
    ]
};

map.users.sort(function(a, b) { 
    return a.id - b.id; 
});

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

5 Comments

What is this compareTo you speak?
compareTo() is a JavaScript function I don't recognize. Is that a Java convention?
@mrblah - Negative, you pass the compare function as the argument.
I think if you just pass in function(a, b) { return (a.username > b.username); } into map.users.sort() -- my example does that.
As @ChaosPandion pointed out, my a.username > b.username is no good. Use a.username.localeCompare(b.username) in the comparison function
-1

You want your object to be an array:

var map = [
    { id: "23434", username: "mrblah" },
    { id: "1010", username: "johnskeet" },
    { id: "1220", username: "alohaguy" }
];

We need a utility to display the usernames in order so we can test our work:

var displayUsernames = function(map) {
    var out = [];
    for (var i=0;i<map.length;i++) {
        out.push((map[i].username));
    }
    alert(out.join(', '));
};

If we use it: displayUsernames(map); we get mrblah, johnskeet, alohaguy

Since it's an array, so we can use .sort(), like this: map.sort();, but if we do that we still get:

mrblah, johnskeet, alohaguy

...from displayUsernames(map); because the array of objects can't be sorted the same way as if it were an array of numbers or strings.

However, if we pass the sort() function a comparison function...

var myCompareFunction = function(a, b) {
    return a.username.localeCompare(b.username);
};

Then pass it into map.sort()

map.sort(myCompareFunction);

Now, when we display it again with displayUsernames(map); we get alohaguy, johnskeet, mrblah

Hope that helps.

6 Comments

The compare function should not return a boolean. developer.mozilla.org/en/Core_JavaScript_1.5_Reference/…
@ChaosPandion - thanks, learn something every day! I changed it from a.username > b.username to a.username.localeCompare(b.username);
I cannot up-vote you, can you make some kind of modification to your answer?
@ChaosPandio - I did modify my answer: stackoverflow.com/revisions/1678007/list ... based on your feedback.
|

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.