Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
2  
possible duplicate of sort outer array based on values in inner array, javascript – Felix Kling Jun 27 '11 at 8:38

3 Answers

up vote 5 down vote accepted
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.

share|improve this answer

You can use this kind 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);

Regards,

Max

share|improve this answer
what is a and b parameters? – gmhk Jun 27 '11 at 8:42
@harigm: sorry, i needed to complete my answer (and i did) – JMax Jun 27 '11 at 8:44
thanks so much it worked – gmhk Jun 27 '11 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.