# How to sort 2 dimensional array by column value?

Can any one help me sort a 2 dimensional Array in JavaScript?

It will have data in the following format:

``````[12, AAA]
[58, BBB]
[28, CCC]
[18, DDD]
``````

It should look like this when sorted:

``````[12, AAA]
[18, DDD]
[28, CCC]
[58, BBB]
``````

So basically, sorting by the first column.

Cheers

It's this simple:

``````var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']];

a.sort(sortFunction);

function sortFunction(a, b) {
if (a[0] === b[0]) {
return 0;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
}
``````

I invite you to read the documentation.

If you want to sort by the second column, you can do this:

``````a.sort(compareSecondColumn);

function compareSecondColumn(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}
``````

The best approach would be to use the following, as there may be repetitive values in the first column.

``````var arr = [[12, 'AAA'], [12, 'BBB'], [12, 'CCC'],[28, 'DDD'], [18, 'CCC'],[12, 'DDD'],[18, 'CCC'],[28, 'DDD'],[28, 'DDD'],[58, 'BBB'],[68, 'BBB'],[78, 'BBB']];

arr.sort(function(a,b) {
return a[0]-b[0]
});
``````

try this

``````//WITH FIRST COLUMN
arr = arr.sort(function(a,b) {
return a[0] - b[0];
});

//WITH SECOND COLUMN
arr = arr.sort(function(a,b) {
return a[1] - b[1];
});
``````

Note: Original answer used a greater than (>) instead of minus (-) which is what the comments are referring to as incorrect.

If you're anything like me, you won't want to go through changing each index every time you want to change the column you're sorting by.

``````function sortByColumn(a, colIndex){

a.sort(sortFunction);

function sortFunction(a, b) {
if (a[colIndex] === b[colIndex]) {
return 0;
}
else {
return (a[colIndex] < b[colIndex]) ? -1 : 1;
}
}

return a;
}

var sorted_a = sortByColumn(a, 2);
``````

Using the arrow function, and sorting by the second string field

``````var a = [[12, 'CCC'], [58, 'AAA'], [57, 'DDD'], [28, 'CCC'],[18, 'BBB']];
a.sort((a, b) => a[1].localeCompare(b[1]));
console.log(a)``````

Nothing special, just saving the cost it takes to return a value at certain index from an array.

``````function sortByCol(arr, colIndex){
arr.sort(sortFunction)
function sortFunction(a, b) {
a = a[colIndex]
b = b[colIndex]
return (a === b) ? 0 : (a < b) ? -1 : 1
}
}
// Usage
var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']]
sortByCol(a, 0)
console.log(JSON.stringify(a))
// "[[12,"AAA"],[18,"DDD"],[28,"CCC"],[58,"BBB"]]"
``````

As my usecase involves dozens of columns, I expanded @jahroy's answer a bit. (also just realized @charles-clayton had the same idea.)
I pass the parameter I want to sort by, and the sort function is redefined with the desired index for the comparison to take place on.

``````var ID_COLUMN=0
var URL_COLUMN=1

findings.sort(compareByColumnIndex(URL_COLUMN))

function compareByColumnIndex(index) {
return function(a,b){
if (a[index] === b[index]) {
return 0;
}
else {
return (a[index] < b[index]) ? -1 : 1;
}
}
}
``````

Standing on the shoulders of charles-clayton and @vikas-gautam, I added the string test which is needed if a column has strings as in OP.

``````return isNaN(a-b) ? (a === b) ? 0 : (a < b) ? -1 : 1 : a-b  ;
``````

The test `isNaN(a-b)` determines if the strings cannot be coerced to numbers. If they can then the `a-b` test is valid.

Note that sorting a column of mixed types will always give an entertaining result as the strict equality test `(a === b)` will always return false. See MDN here

This is the full script with Logger test - using Google Apps Script.

``````function testSort(){

function sortByCol(arr, colIndex){
arr.sort(sortFunction);
function sortFunction(a, b) {
a = a[colIndex];
b = b[colIndex];
return isNaN(a-b) ? (a === b) ? 0 : (a < b) ? -1 : 1 : a-b  ;  // test if text string - ie cannot be coerced to numbers.
// Note that sorting a column of mixed types will always give an entertaining result as the strict equality test will always return false
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

}
}
// Usage
var a = [ [12,'12', 'AAA'],
[12,'11', 'AAB'],
[58,'120', 'CCC'],
[28,'08', 'BBB'],
[18,'80', 'DDD'],
]
var arr1 = a.map(function (i){return i;}).sort();  // use map to ensure tests are not corrupted by a sort in-place.

Logger.log("Original unsorted:\n     " + JSON.stringify(a));
Logger.log("Vanilla sort:\n     " + JSON.stringify(arr1));
sortByCol(a, 0);
Logger.log("By col 0:\n     " + JSON.stringify(a));
sortByCol(a, 1);
Logger.log("By col 1:\n     " + JSON.stringify(a));
sortByCol(a, 2);
Logger.log("By col 2:\n     " + JSON.stringify(a));

/* vanilla sort returns " [
[12,"11","AAB"],
[12,"12","AAA"],
[18,"80","DDD"],
[28,"08","BBB"],
[58,"120","CCC"]
]
if col 0 then returns "[
[12,'12',"AAA"],
[12,'11', 'AAB'],
[18,'80',"DDD"],
[28,'08',"BBB"],
[58,'120',"CCC"]
]"
if col 1 then returns "[
[28,'08',"BBB"],
[12,'11', 'AAB'],
[12,'12',"AAA"],
[18,'80',"DDD"],
[58,'120',"CCC"],

]"
if col 2 then returns "[
[12,'12',"AAA"],
[12,'11', 'AAB'],
[28,'08',"BBB"],
[58,'120',"CCC"],
[18,'80',"DDD"],
]"
*/

}
``````

in one line:

``````var cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
]

function myFunction() {
return cars.sort((a, b)=> a.year - b.year)
}
``````

If you want to sort based on first column (which contains number value), then try this:

``````arr.sort(function(a,b){
return a[0]-b[0]
})
``````

If you want to sort based on second column (which contains string value), then try this:

``````arr.sort(function(a,b){
return a[1].charCodeAt(0)-b[1].charCodeAt(0)
})
``````

P.S. for the second case, you need to compare between their ASCII values.

Hope this helps.