JS Multidimensional array sorting

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax array button data function html http javascript

JS multidimensional array Sorting is the focus of this article, before only talking about array ordering, now is the implementation of multidimensional data sorting, JS multidimensional array can be understood as a one-dimensional array, each element of it is an array object, and so on

<!doctype HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ""
<html xmlns= "http://www.w3.org/1999/xhtml"
<head>
<meta http-equiv= "Content-type" content= "HTML; charset=gb2312 "/>
<title>js multidimensional array </title>
<script language= web Effects
var myarray = new Array ();     
for (Var i=0;i<10;i++) {
Myarray[i]=new array ();
    Myarray[i][0]=math.floor (Math.random () AOE);
Myarray[i][1]=math.floor (Math.random () AOE);
Myarray[i][2]=math.floor (Math.random () AOE);
Myarray[i][3]=math.floor (Math.random () AOE);
Myarray[i][4]=math.floor (Math.random () AOE);
Myarray[i][5]=math.floor (Math.random () AOE);
Myarray[i][6]=math.floor (Math.random () AOE);
Myarray[i][7]=math.floor (Math.random () AOE);
Myarray[i][8]=math.floor (Math.random () AOE);
}

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>

</head>

<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>
</html>

Related Article

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.