Multiple uses of sort ()

Source: Internet
Author: User

The sort () method is used to sort elements of an array.

First, the default situation

By default, the sort () method arranges array items in ascending order. In order to implement sorting, the sort () method invokes the toString () transformation method for each array item, and then compares the resulting string to determine how to sort. As follows:

1 var values = ["Orange", "apple", "banana"]; 2 Values.sort (); 3 Console.log (values); //Results ["Apple", "banana", "orange"]

However, even if each item in the array is a numeric value, the sort () method compares the string as follows:

1 var values = [0,1,5,10,15]; 2 Values.sort (); 3 Console.log (values); // results [0, 1, ten, 5]

Second, the value of the order

The sort () method can receive a comparison function as a parameter.

The comparison function receives two parameters and returns a negative number if the first argument should precede the second parameter, or 0 if the two arguments are equal, and returns a positive number if the first argument is in the second one.

1 function Compare (A, b) {2     return (A- b); 3 }//comparison function in ascending order 4var values = [0,1,5,10,15]; 5 Values.sort (compare); 6 Console.log (values); // results [0, 1, 5, ten,.]

Three, sorting according to an object property array

Define a function that receives a property name and then creates a comparison function based on the property name. Here is the definition of this function:

1 functioncreatecomparisonfunction (PropertyName) {2     return function(Object1, object2) {3         varA =Object1[propertyname];4         varb =Object2[propertyname];5 6         if(A <b) {7             return-1;8}Else if(A >b) {9             return1;Ten}Else{ One             return0; A         } -     } -}//returns a comparison function in ascending order created from a property name

After the intrinsic function receives the propertyname parameter, it uses square brackets notation to get the value of the given property.

The above function can be used in this way, as in the following example.

1 var data = [{name: "Lily", Age:12}, {name: "Judy", age:22}]; 2 Data.sort (createcomparisonfunction ("name")); 3 Console.log (data[0].name); // Judy 4 5 Data.sort (createcomparisonfunction ("Age")); 6 Console.log (data[0].name); // Lily

Multiple uses of sort ()

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.