JavaScript to implement multi-key sorting method of multidimensional array

Source: Internet
Author: User
Tags array sort sort

The array sort function of JavaScript is sorted by default in ascending order of ASCII characters.

Arrayobj.sort (sortfunction);

Parameters: Sortfunction

Options available. is the name of the function used to determine the order of elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.

The sort method sorts the array object appropriately, and does not create a new array object during execution.

If you provide a function for the sortfunction parameter, the function must return one of the following values:

Negative value if the first argument passed is smaller than the second argument.

0 if two parameters are equal.

Positive value if the first argument is larger than the second argument.

The above method is easy to sort in one dimension, but how do you sort by the same multiple-key values as the order in SQL statements?

Multiple-key ordering of multidimensional arrays requires more complexity, but does not require a circular resolution. The truth of the actual solution is the same.

Digital:

The following example is to sort the multidimensional array of numbers by the order of the 5th, 9th, and 3rd columns, as in SQL statements by COL5,COL9,COL7. A number can be subtracted directly from two items, with the result as the return value.

<script language=javascript>
   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>

Character:

character, items in sortfunction cannot be subtracted directly from the number, and need to be called

Str1.localecompare (str2) method to compare to satisfy the return value. The following is the case where the 1th, 2 columns of a multidimensional array are sorted.

function sortFunction(array) ...{
   return array.sort( function(x, y) ...{
   return (x[0]==y[0])?(x[1].localeCompare(y[1])):(x[0].localeCompare(y[0]))
   });
}

Therefore, the sorting function of Arrayobject.sort (sortfunction) is still very powerful, and finally it can realize the function of order by as in SQL statement.

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.