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 multidimensional array by sum

/*//*\___________________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 :: js is number 
Javascript :: js on dom content loaded 
Javascript :: js trigger mouseover 
Javascript :: clear canvas 
Javascript :: javascript object array merge 
Javascript :: javascript clear classlist 
Javascript :: datepicker get selected date 
Javascript :: express js cors 
Javascript :: javascript copy an array 
Javascript :: javascript create array of objects with map 
Javascript :: get keys wher value is true in object in javascript 
Javascript :: javascript remove first character from string 
Javascript :: store array in localstorage 
Javascript :: js date after 1 year 
Javascript :: importing svg into react 
Javascript :: get only day from date in javascript 
Javascript :: javascript not empty array not string 
Javascript :: get current directory vbscript 
Javascript :: for array javascript 
Javascript :: for each element in object 
Javascript :: ngingx proxy express get real ip 
Javascript :: how to get current date in js 
Javascript :: javascript is valid json string 
Javascript :: find email domain javascript 
Javascript :: js remove first and last element from array 
Javascript :: javascript regex extract url from string 
Javascript :: how to change h1 color in javascript 
Javascript :: how to set state when change viewport react 
Javascript :: back button js 
Javascript :: javascript string pop 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =