JS Multidimensional array sorting

Source: Internet
Author: User

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>js Multidimensional Array Ordering </title>
<script language= Web page Special effects >
var myarray = new Array ();
for (Var i=0;i<10;i++) {
Myarray[i]=new Array ();
Myarray[i][0]=math.floor (Math.random () *10);
Myarray[i][1]=math.floor (Math.random () *10);
Myarray[i][2]=math.floor (Math.random () *10);
Myarray[i][3]=math.floor (Math.random () *10);
Myarray[i][4]=math.floor (Math.random () *10);
Myarray[i][5]=math.floor (Math.random () *10);
Myarray[i][6]=math.floor (Math.random () *10);
Myarray[i][7]=math.floor (Math.random () *10);
Myarray[i][8]=math.floor (Math.random () *10);
}
  
Myarray.sort (function (x, y) {
Return (X[0]==y[0])? ((X[4]==y[4])? (X[8]-y[8]):(x[4]-y[4])):(x[2]-y[2])
});
for (Var i=0;i<myarray.length;i++) {
document.write (Myarray[i].join (",") + "<br/>");
}
</script>
Again a classic JS multidimensional data sorting Method code bar

var temp = [
{x:x-1, y:y-1, Ct:5, d:0},
{x:x, y:y-1, Ct:2, d:1},
{x:x+1, y:y-1, Ct:7, d:2},
{x:x+1, y:y, Ct:3, D:3},
{x:x+1, y:y+1, ct:0, D:4},
{x:x, y:y+1, Ct:1, D:5},
{x:x-1, y:y+1, Ct:6, D:6},
{x:x-1, y:y, Ct:4, D:7}
];

1, the first thing you give is not a multidimensional array, JS also has no multi-dimensional array of Concepts
2, simple application using bubble method is sufficient, the data can be larger when the partition method and Quick Sort Method
3, here gives the bubble method + callback function code, example for the array in ascending order of CT column
<script>
function   cmp (a,b,c)    {
    if a[c]   ==    B[c])
        return   0;
    return   a[c]   >   b[c]  ?   1:-1;
}

for (I=0;i <temp.length-1;i++)
for (J=i;j <temp.length;j++)
if (CMP (temp[i],temp[j], "CT") > 0) {
c = Temp[i]
Temp[i] = Temp[j]
TEMP[J] = C
}
</script>

<body>
<body>
<div   id=div1> </div> <br>
<button    Onclick=mysort (0) > Sortbycol-1 </button>
<button   onclick=mysort (1) > Sortbycol-2 </button>
<button   onclick=mysort (2) > sortbycol-3 </button>
< button   Onclick=mysort (3) > sortbycol-4 </button>
<script   language= JavaScript ">
<!--
var   ar=[[75,86,95,64],[66,88,77,99],[86,45,76,85],[94,65,86,70]]
div1.innerhtml   =   ar.join ("<br>")

var col=0;
Function cmp (A,B) {
return A[col]-b[col];
}
function Mysort (i) {
col = i;
AR = Ar.sort (CMP);
div1.innerhtml = Ar.join ("<br>")
}
-->
</script>
</body>


</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.