Sorting Two Dimensional Arrays

Discussion in 'Web Design, HTML And CSS' started by sen_kumar76, Jan 8, 2008.

  1. sen_kumar76

    sen_kumar76 New Member

    Joined:
    Nov 28, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.funmin.com
    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
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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>
    
     
  3. sen_kumar76

    sen_kumar76 New Member

    Joined:
    Nov 28, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.funmin.com
    thanks working
    Regards,
    Senthil Kumar D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice