6

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

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

@harigm: sorry, i needed to complete my answer (and i did)
2
 ary.sort(function(x,y) { return x[1].localeCompare(y[1]) })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.