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 :: angular playground online 
Javascript :: how to draw a flower in javascript 
Javascript :: repeating countdown timer javascript 
Javascript :: chunking array javascript 
Javascript :: bind json data to dropdownlist using javascript 
Javascript :: Update failed: ChunkLoadError: Loading hot update chunk app failed. 
Javascript :: slider js 
Javascript :: look behind regex 
Javascript :: app script append two list 
Javascript :: impress.js cdn 
Javascript :: how to edit a fil with vanilla js 
Javascript :: vue state 
Javascript :: vscode format - .prettierrc jsx singleQuote not work 
Javascript :: react particles js 
Javascript :: javascript eingabe in inputfielder übernehmen 
Javascript :: draft js insert text example 
Javascript :: qr code terminal npm 
Javascript :: javascript string objects 
Javascript :: moment.js get time from now 
Javascript :: promise syntax in js 
Javascript :: box shadow generador react native 
Javascript :: axios.create 
Javascript :: react-beforeunload react navive 
Javascript :: how to connect ms access database in html using javascript 
Javascript :: Angular Mat-Table with Dynamic Columns generate and Data should be populated in horizontal way 
Javascript :: req.params 
Javascript :: how to search for react icons on vscode 
Javascript :: react hero slider 
Javascript :: javascript sorting an array 
Javascript :: pass element from child to parent react 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =