Home  >  Article  >  Web Front-end  >  Sorting method of JavaScript object array_javascript skills

Sorting method of JavaScript object array_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:35:531340browse

The example in this article describes the sorting method of JavaScript object array. Share it with everyone for your reference, the details are as follows:

Javascript's array sorting function sort method, by default, sorts in ascending order according to ASCII character order.
arrayobj.sort(sortfunction);

Parameter: sortFunction

Optional. is the name of the function used to determine the order of elements. If this parameter is omitted, the elements will be sorted in ascending ASCII character order.

The

sort method sorts Array objects appropriately; no new Array objects are created during execution.

If a function is provided for the sortfunction argument, the function must return one of the following values:

Negative value if the first parameter passed is smaller than the second parameter.
Zero if both arguments are equal.
Positive value if the first argument is greater than the second argument.

The above method is very convenient for one-dimensional sorting, but how to do multi-key value sorting like ORDER BY in SQL statements?

Multi-key value sorting of multi-dimensional arrays needs to be more complicated, but there is no need to use loops to solve it. The actual solution is the same.

Number:

The following example is to sort a multi-dimensional array of numbers in the order of column 5, column 9, and column 3, like ORDER BY col5, col9, col7 in the SQL statement. When it comes to numbers, you can directly subtract the two items and use 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) {
    if(x[4]!=y[4]){
      return x[4]-y[4];
    } else if(x[8]!=y[8]){
      return x[8]-y[8];
    } else if(x[6]!=y[6]){
      return x[6]-y[6];
    } else {
      return 1;
    }
  }
  );
 for(var i=0;i<myArray.length;i++ )...{
 document.write(myArray[i].join(",") + "<br/>");
 }
</script>

Character:

characters, the items in sortFunction cannot be directly subtracted like numbers. You need to call the str1.localeCompare(str2) method for comparison to satisfy the return value. The following is the case of sorting columns 1 and 2 of a multidimensional array.

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

So the sorting function of arrayObject.sort(sortFunction) is still very powerful, and it can finally realize the same function as ORDER BY in SQL statements.

I hope this article will be helpful to everyone in JavaScript programming.

Statement:
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