A simple example code for JS two-dimensional array ordering

Source: Internet
Author: User
Tags ord

  This article is mainly on the JS two-dimensional array sorting Simple sample code is introduced, the need for friends can come to the reference, I hope to help you.

As follows:     code is as follows: <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > <html> & nbsp;<head>   <title> New Document </title>   <meta name= "generator" content= "EditPlus" & Gt   <meta name= "Author" content= ">   <meta name=" Keywords "content=" ">   <meta name=" Description "content=" ">   <script type=" Text/javascript ">   <!--   function Getcard (M {  var ret = [], rnd;    for (var i = 0; i < m; i++) {   RND = Math.floor (Math.random () * (i+0. 9999));    ret[i] = ret[rnd];    RET[RND] = i  }   return ret; &NBSP}    var data = [];  {  for (var i = 0; i < i + +) {   data.push (getcard);  }  }  //two-dimensional data ordering &NB Sp;function mysort (Array, col, Ord, B, e) {  if (array.length = 0) return;  //document.write ("Pre-sorted" +array); &nbSp var t = [], S, i, Curcol = col[0];   for (i = b; i < E; i + +) {   s = new String (Array[i][curcol]);    s._obj = Array[i];   & Nbsp;t.push (s)  }   T.sort ();   if (ord[0] = = "Des") {   t.reverse ();  }   for (i = 0; i < e-b; i++) {   //docume Nt.write ("<br/>");    //document.write (Array[i]);    //document.write ("<br/>");    //document.write (t[i]._obj);    //document.write ("<br/>");    //document.write (T[i]._obj = = Array[b+i]);    array[b+i] = t[i]._obj;  }  //document.write ("sorted after ====================<br/>")  //print (array);   VAR begin, end;   if (Col.length > 1) {   col.shift ();    ord.shift ();    begin = b;    for (i = b; i < e-b-1; i++) {    if (Array[i][curcol]!= Array[i+1][curcol]) {     end = i+1;      //document.write ("begin=" +begin+ "end=" +end+ "<br/>");      mysort (array,col,ord,begin,end);      begin = end;    }   &NBSP}    if (begin!= E) {    mysort (array,col,ord,begin,e);    } &N Bsp }    //document.write ("<br/>");  //document.write ("sort after" +array); &NBSP}  function Print (data) {  for (var i = 0; i < data.length; i++) {   document.write (data[i]+ "& Lt;br/> ");  } &nbsp}  document.write ("====================<br/> before sorting");  print (data);  var B = new Date (). GetTime ();;  mysort (data, [0,1,2],[' ASC ', ' des ', ' ASC '],0,data.length);   document.write ("spents" + (new Date ()). GetTime ()-B));  document.write ("sorted after ====================<br/>")  print (data);  //-->   </script>  </head>    <body>    </body> </ Html>  

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.