Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   Sorting Two Dimensional Arrays (http://www.go4expert.com/forums/sorting-dimensional-arrays-t8158/)

sen_kumar76 8Jan2008 17:07

Sorting Two Dimensional Arrays
 
hi

how to sort Two Dimensional array in javascript?


my array is 4,8
5,9
2,10

then result is 2,10
4,8
5,9

the 0th cols can be sort


Thanks in advance
Sen_kumar76

pradeep 8Jan2008 21:48

Re: Sorting Two Dimensional Arrays
 
Code: HTML

<html>
<head>

<SCRIPT LANGUAGE="JavaScript">

function sortMultiDimensional(a,b)
{
    // this sorts the array using the second element   
    return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0));
}
</SCRIPT>
</head>

<body>
<script LANGUAGE="JavaScript">
var arr = [[4,8],[5,9],[2,10]]; // the multidimensional array
//arr.sort(); // normal sort
arr.sort(sortMultiDimensional); // sort using our custom function

for(var i=0;i<arr.length;i++)
{
    document.write(arr[i].toString()+"<br>");
}
</script>

</body>
</html>


sen_kumar76 10Jan2008 17:09

Re: Sorting Two Dimensional Arrays
 
thanks working
Regards,
Senthil Kumar D


All times are GMT +5.5. The time now is 21:04.