Sort 2d Array by Column
Wed Feb 09 2022 19:12:53 GMT+0000 (Coordinated Universal Time)
Saved by
@MakiProductions
/*
Sort 2D array by column
PASS: theArray (a 2D array), sortColumn(the column to sort by), sortAsString (if true, converts the value to a string before comparing)
RETURNS: theArray, sorted by the column specified
*/
function sort2DArray (theArray, sortColumn, sortAsString = false) {
theArray = theArray.sort(function(x,y){
if (sortAsString) {
var xp = x[sortColumn].toString();
var yp = y[sortColumn].toString();
} else {
var xp = x[sortColumn];
var yp = y[sortColumn];
}
return xp == yp ? 0 : xp < yp ? -1 : 1;
});
return theArray;
}
content_copyCOPY
Comments