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

/*//*\___________________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 diffence between a++ and ++a 
Javascript :: submit form jquery browser check 
Javascript :: connect to redux store outside component 
Javascript :: js how to filter range imutable array 
Javascript :: listen for double click before click 
Javascript :: localstorage getitem 
Javascript :: what is vue.js 
Javascript :: toast notification angular bootstrap 8 
Javascript :: reacts mos twanted 
Javascript :: perfect scrollbar jquery 
Javascript :: notify jquery 
Javascript :: ejs 
Javascript :: Delete - Cloudinary 
Javascript :: npm redis for js 
Javascript :: express generator error handling 
Javascript :: Limit number of selected chekboxes 
Javascript :: get element by id two ids 
Javascript :: javascript closure interview questions 
Javascript :: double ?? js 
Javascript :: webpack.config.js 
Javascript :: global catch in javascript 
Javascript :: react switch case 
Javascript :: of rxjs 
Javascript :: react use component 
Javascript :: currenttarget javascript 
Javascript :: js ?. 
Javascript :: process.env type 
Javascript :: how to check request type in js 
Javascript :: toastify js 
Javascript :: how to uninstall nodejs web server 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =