js二維數組排序的簡單範例程式碼

來源:互聯網
上載者:User

 本篇主要是對js二維數組排序的簡單範例程式碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所協助

如下所示:  代碼如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta name="Generator" content="EditPlus">  <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; }  var data = []; {  for(var i = 0; i < 100; i ++){   data.push(getCard(10));  } } // 二維資料排序 function mySort(array, col, ord, b, e){  if(array.length == 0) return;  //document.write("排序前"+array);  var t = [], s, i, curCol = col[0];  for(i = b; i < e; i ++){   s = new String(array[i][curCol]);   s._obj = array[i];   t.push(s)  }  t.sort();  if(ord[0] == "des"){   t.reverse();  }  for(i = 0; i < e - b; i++){   //document.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("排序後====================<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;    }   }   if(begin != e){    mySort(array,col,ord,begin,e);   }  }   //document.write("<br/>");  //document.write("排序後"+array); } function print(data){  for(var i = 0; i < data.length; i++){   document.write(data[i]+"<br/>");  } } document.write("排序前====================<br/>"); print(data); var b = new Date().getTime();; mySort(data, [0,1,2],['asc','des','asc'],0,data.length);  document.write("用時"+(new Date().getTime() - b)); document.write("排序後====================<br/>") print(data);  //-->  </script> </head>  <body>  </body></html> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.