About sort Sorts

Source: Internet
Author: User
Tags array sort sorts

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

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.