Sorting a 2-D Javascript Array

Can someone help me in sorting a 2 dimensional array

Which will have data in the following format

[2, All are fine] [4, All is Well] [1, Welcome Code] [9, Javascript] After sorting it should look like [2, All are fine] [4, All is Well] [9, Javascript] [1, Welcome Code] 

The main thing I'm focusing on is sorting based on Text not on ID

+6
source share
3 answers
 ary.sort(function(a, b) { return (a[1] < b[1] ? -1 : (a[1] > b[1] ? 1 : 0)); }); 

See http://jsfiddle.net/tdBWh/ for this example and MDC for documentation .

+10
source

You can use this type of code:

 function sortMultiDimensional(a,b) { // for instance, this will sort the array using the second element return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0)); } 

and then use the sort method:

 myArray.sort(sortMultiDimensional); 

Hi,

Max

+3
source

Source: https://habr.com/ru/post/891428/


All Articles