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 2d array

/*//*\___________________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

how to sort a 2D array in 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

PREVIOUS NEXT
Code Example
Javascript :: javascript sort multidimensional array by sum 
Javascript :: angular online editor 
Javascript :: ckeditor ignore contenteditable 
Javascript :: Towers of Hanoi, Iterative and Recursive 
Javascript :: copy array of object in js 
Javascript :: javascript url 
Javascript :: js insert in array 
Javascript :: angular 11 support versions nodejs 
Javascript :: puppeteer click element with custom property 
Javascript :: script src in folder 
Javascript :: material ui change icon size on xs screen 
Javascript :: react native layout animation android 
Javascript :: node express config file json 
Javascript :: javascript check if string is empty 
Javascript :: if isset handlebars js 
Javascript :: express sendfile root path 
Javascript :: isodate mongodb nodejs 
Javascript :: how to print 1 to 10 table in javascript 
Javascript :: sum array elements in javascript 
Javascript :: display none y display block infinito con javascript 
Javascript :: ionic not compiling with proxy 
Javascript :: create a component in react 
Javascript :: how to stop overlapping divs to interact with each others click events 
Javascript :: js alert with multiple buttons 
Javascript :: express json middleware 
Javascript :: one signal api to send notification 
Javascript :: javascript find missing number 
Javascript :: visual studio node.js mac 
Javascript :: jquery save method 
Javascript :: javascript cheatsheet 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =