Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting 2 dimensional Javascript array

Tags:

javascript

Can any one help me in sorting a 2 Dimensional Array

Which will have the 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]

Main thing that i am focussing is to sort based on Text not on the ID

like image 350
gmhk Avatar asked Jun 27 '11 08:06

gmhk


People also ask

How do you sort values in a 2D array?

To sort all elements of a 2D array by row-wise. As in the above rewrite program, the sort() method is used to iterate each element of a 2D array and sort the array row-wise. Finally, the print method displays all the elements of the 2D array.

Can you sort a 2D array?

In Java, a 2D array can be sorted row-wise or column-wise as per requirements. For row-wise sorting, only the Array. sort() method is utilized; however, in column-wise sorting, the Array. sort() method is called with the Comparator interface.

Are there 2D arrays in JavaScript?

No, it's not. In some languages, you can have multidimensional arrays like string[3,5] = "foo"; .


1 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.

like image 196
Gijs Avatar answered Sep 19 '22 14:09

Gijs