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.
var map = [ { id: "23434", username: "mrblah" }, { id: "1010", username: "johnskeet" } ];