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 :: validate mobile number in javascript 
Javascript :: import a script to my react componetn 
Javascript :: how to turn number into string javascript 
Javascript :: downgrade node version windows using npm 
Javascript :: chalk nodeks 
Javascript :: firestore set a document 
Javascript :: joi custom error 
Javascript :: vuex add multiple payload to mutation 
Javascript :: js get last array element 
Javascript :: jquery add event after page load 
Javascript :: Javascript form check to see if return or enter was pressed 
Javascript :: angular json to file and download 
Javascript :: javascript localstorage 
Javascript :: react native floating button 
Javascript :: typeorm subquery 
Javascript :: npm numeral 
Javascript :: map javascript index 
Javascript :: routes react 
Javascript :: check if numbers are consecutive javascript 
Javascript :: for object 
Javascript :: javascript to remove few items from array 
Javascript :: to uppercase js 
Javascript :: javascript take last element of array 
Javascript :: first repeated character in a string javascript 
Javascript :: convert jquery fadeOut function to pure javascript code 
Javascript :: object fromentries example 
Javascript :: convert int to float in javascript 
Javascript :: set default date today js 
Javascript :: MongoParseError: options buffermaxentries, usefindandmodify, usecreateindex are not supported 
Javascript :: set attribute in javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =