Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort multidimensional array javascript

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]
});
Comment

javascript sort multidimensional array

let 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']];

// sort by first column (numbers) using minus
arr.sort((a, b) => a[0]-b[0]);
/* [[12,"AAA"],[12,"BBB"],[12,"CCC"],[12,"DDD"],[18,"CCC"],[18,"CCC"],[28,"DDD"],[28,"DDD"],[28,"DDD"],[58,"BBB"],[68,"BBB"],[78,"BBB"]] */

// sort by second column (letters) using localeCompare
arr.sort((a, b) => a[1].localeCompare(b[1]));
/* VM704:1 [[12,"AAA"],[12,"BBB"],[58,"BBB"],[68,"BBB"],[78,"BBB"],[12,"CCC"],[18,"CCC"],[18,"CCC"],[12,"DDD"],[28,"DDD"],[28,"DDD"],[28,"DDD"]] */
Comment

javascript sort multi-dimensional array by column

/*//*\___________________l4r3f4c3@proton.me________________/|**/
/**/  Array.prototype.sort2d = function(column) {              //
/**/  column = typeof column === "undefined" ? 0 : column      // 
/**/  return this.sort((a, b) => a[column]-b[column]); }       //
/**/  const unsorted2DArray = [[3,1,1,1],[9,3,3,3],[6,2,2,2]]  //
/**/  sorted2DArray = unsorted2DArray.sort2d()                 //
/**/  //sorted2DArray = [ [3,1,1,1],[6,2,2,2],[9,3,3,3] ]      //
/*//*======================/*//*/=========================/||*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript sort array 
Javascript :: transaction commit rollback nodejs 
Javascript :: hide and open jquery 
Javascript :: jquery event delegation 
Javascript :: what is on and once in node 
Javascript :: vue route automatic redirect 
Javascript :: angular json and cli json file 
Javascript :: customize function (doc) datatable printable 
Javascript :: en eternal gloden braid 
Javascript :: homepage field in package.json 
Javascript :: JSE Data 
Javascript :: angularjs - controllerAs 
Javascript :: how to make data toggle=modal always active 
Javascript :: interactive svg javascript 
Javascript :: check uncek react-bootstrap-table reactjs 
Javascript :: GET and CHANGE the class of an element 
Javascript :: pagination react 
Javascript :: chrome resize window javascript 
Javascript :: The element.style Property 
Javascript :: how to search and filter js 
Javascript :: js detect mouse support 
Javascript :: get query params from url 
Javascript :: how to creacte react component 
Javascript :: ternary operator nodejs 
Javascript :: axios cors http localhost forbidden 
Javascript :: jquery numeric validation 
Javascript :: google map get lat long by pincode 
Javascript :: if statemnt shorthand js without else 
Javascript :: solcjs 
Javascript :: find key in nested json object 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =