Sorting in JavaScript

12 April 2010 ~ blog javascript

Every now and then you need to sort something in JavaScript and though, it's not all that hard to do, it is not the best-documented feature. Here is a quick little summary of how to do it.

You call the sort function of an array with your comparator. A JavaScript comparator is just a function that returns -1, 0, or 1 depending on whether a is less than b, a is equal to b, or a is greater than b:

myarray.sort(function(a,b){
    if(a < b){
        return -1;
    } else if(a == b){
        return 0;
    } else { // a > b
        return 1;
    }
});

This is just an example, your function can base the comparison on whatever you want, but it needs to return -1,0,1. Say you had a set of custom JavaScript objects that you want sorted by age:

var people = [{name:'Bob',age:21}, {name:'Fred',age:34}, {name:'Dan',age:19}];

You could easily sort them using

people.sort(function(a,b){
    if(a.age < b.age){
        return -1;
    } else if(a.age == b.age){
        return 0;
    } else { // a > b
        return 1;
    }
});

Not too hard to do. It's actually very similar to the Java Comparator interface.


Creative Commons License CoffeaElectronica.com content is copyright © 2016 Christopher J. Stehno and available under a Creative Commons Attribution-ShareAlike 4.0 International License.