venerdì, novembre 05, 2010

Sorting a Multidimensional array in JavaScript

I’m noting this for myself, but I’d recommend it to all: a very useful routine courtesy of Stack Overflow:

var arr = [.....]
arr.sort((function(index){
    return function(a, b){
        return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
    };
})(2)); // 2 is the index