Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort string 2d array in javascript

var arr1 = [[1, "Hair Pin"], [21, "Bowling Ball"], [2, "Dirty Sock"], [5, "Microphone"]];

arr1.sort(function (a, b) {
    return a[1].localeCompare(b[1]);
});

document.write('<pre>' + JSON.stringify(arr1, 0, 4) + '</pre>');
 Run code snippet
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 :: get combinations of two js 
Javascript :: moment get weekday name 
Javascript :: aos animation angular 
Javascript :: js trigger window resize 
Javascript :: js string to regex 
Javascript :: contains whitespace js function 
Javascript :: ReferenceError: primordials is not defined 
Javascript :: ACCESS IFRAME INNER HTML IN CHROME CONSOLE 
Javascript :: javascript remove item onclick 
Javascript :: remove brackets from array javascript 
Javascript :: javascript get image width and height 
Javascript :: how to remove empty spaces befiore string js 
Javascript :: javascript ajax load html into div 
Javascript :: jquery div element find and remove 
Javascript :: get date javascript 
Javascript :: disable global require eslint 
Javascript :: javascript constructor function vs factory function 
Javascript :: output in javascript 
Javascript :: toggle class javascript 
Javascript :: get highest value in array of object javascript 
Javascript :: add javascript keyup on input 
Javascript :: jquery find selected option by class 
Javascript :: how to hide component in react 
Javascript :: error vuejs from chokidar enospc 
Javascript :: js conditional object key 
Javascript :: Creating a Proxy Object 
Javascript :: stylelint default config 
Javascript :: get object key from value javascript 
Javascript :: trigger play video jquery 
Javascript :: object to array javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =