Home > Web Front-end > JS Tutorial > body text

Simple sample code for sorting two-dimensional arrays in js

PHPz
Release: 2018-09-30 15:58:05
Original
1268 people have browsed it

This article mainly introduces the simple sample code of js two-dimensional array sorting. Friends who need it can come and refer to it. I hope it will be helpful to everyone

As shown below:

<!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],[&#39;asc&#39;,&#39;des&#39;,&#39;asc&#39;],0,data.length); 
 document.write("用时"+(new Date().getTime() - b));
 document.write("排序后====================<br/>")
 print(data);
  </script>
 </head>
 <body>

 </body>
</html>
Copy after login

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!