I have a list of data objects to sort. I want to do something like
list.sort(function(item1, item2){
return item1.attr - item2.attr;
})
Well, this ought to work:
if ( item1.attr < item2.attr )
return -1;
if ( item1.attr > item2.attr )
return 1;
return 0;
[Edit: uh, and I guess you could also use localeCompare()
:
return item1.attr.localeCompare(item2.attr);
I did not previously know about it. Heh, cool, learned something. ]