How to Sort an Array Of Arrays in Javascript

Hello, its very easy to to sort an array of arrays you just need to use the special function sort of javascript.

Let see, fist the array to order

var points = [[40,1],[100,2],[1,3],[5,4],[25,5],[10,6]];

Then code for sort the points array

var points = [[40,1],[100,2],[1,3],[5,4],[25,5],[10,6]];
points.sort(
    function(a,b)
    {
        return a[0]-b[0]
    }
);

Result:[[1,3],[5,4],[10,6],[25,5],[40,1],[100,2]]

To sort the points array ascending by the first element in the every point array, if you want to change it to sort descending the array, just need to change the order of the values.

var points = [[40,1],[100,2],[1,3],[5,4],[25,5],[10,6]];  
points.sort(  
    function(a,b)
    {
        return b[0]-a[0]
    }
)

Result:[[100,2],[40,1],[25,5],[10,6],[5,4],[1,3]]