The array sort function of JavaScript is sorted by default in ascending order of ASCII characters.
Arrayobj.sort (sortfunction);
Parameter: sortfunction
Options are available. is the name of the function used to determine the order of the elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.
The sort method sorts the array objects 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:
A negative value, if the first argument passed is smaller than the second one.
0, if two parameters are equal.
Positive value if the first argument is larger than the second argument.
Learn about the sort function with practical examples
1. String sorting
varFruits=["Banana","Orange","Apple","Mango"];
Fruits.Sort();//Sort result is apple,banana,mango,orange
To get the results orange,mango,banana,apple only need to reverse the fruits results from the previous step
Fruits.Reverse();//Sort result is orange,mango,banana,apple
2. Sorting by Numbers
From small to large
varPoints=[ +, -,1,5, -,Ten];
Points.Sort(function(A, b){returnA-B});//Sort result is 1,5,10,25,40,100
From big to small
varPoints= [ +, -,1,5, -,Ten];
Points.Sort(function(A, b){returnB-A});//Sort result is 100,40,25,10,5,1
The above method is very convenient in one-dimensional sorting, but how do you sort the multi-key values like the order by in the SQL statement?
Multi-key ordering of multidimensional arrays requires a bit more complexity, but does not need to be solved with loops. The truth is the same as the actual solution.
Digital:
The following example is a multidimensional array of numbers that follows the 3rd column, like an order by Col in an SQL statement. Numbers can be directly subtracted from two items, with the result as the return value.
<script language=javascript>
varMyArray=NewArray();
for(varI=0; I<Ten; I++){
MyArray[I]=NewArray();
MyArray[I][0]=Math. Floor(Math.Random()*Ten);
MyArray[I][1]=Math. Floor(Math.Random()*Ten);
MyArray[I][2]=Math. Floor(Math.Random()*Ten);
MyArray[I][3]=Math. Floor(Math.Random()*Ten);
MyArray[I][4]=Math. Floor(Math.Random()*Ten);
MyArray[I][5]=Math. Floor(Math.Random()*Ten);
MyArray[I][6]=Math. Floor(Math.Random()*Ten);
MyArray[I][7]=Math. Floor(Math.Random()*Ten);
MyArray[I][8]=Math. Floor(Math.Random()*Ten);
}
MyArray.Sort(function(X, y){
return(X[2]-Y[2])
});
for(varI=0; I<Myarray.length;i++){
Document.Write(MyArray[I].Join(",")+"<br/>");
}
</script>
Character:
Characters, items in the sortfunction cannot be subtracted directly like numbers and need to be called
The Str1.localecompare (str2) method is used for comparison, thus satisfying the return value. The following is the case for sorting the 1th and 2 columns of a multidimensional array.
functionSortfunction(Array) {
returnArray.Sort(function(X, y){
return(X[0]==Y[0])?(X[1]. localecompare(Y[1])):(X[0]. localecompare(Y[0]))
});
}
About sort Sorts