Built-in sorting function of JavaScript Array

Source: Internet
Author: User
Tags sorts javascript array

The javascript built-in sort function is a set of multiple sorting algorithms.

JavaScript sorts multi-dimensional arrays and object arrays. It uses the native sort () method to sort array elements.

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

ArrayObject. sort (order );

Test A: var mm = [, 2]; mm. sort (); alert (mm); // 0 1 1 2 3 4 6

The returned value is an array reference. Note that arrays are sorted on the original array without generating copies.

If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this, first convert the elements of the array into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number indicating the relative sequence of the two values. The comparison function should have two parameters, a and B. The returned values are as follows:

  • If a is less than B, a should appear before B in the sorted array, then a value smaller than 0 is returned.
  • If a is equal to B, 0 is returned. A and B are considered equal
  • If a is greater than B, a value greater than 0 is returned. B should be behind

Test B: function NumAscSort (a, B) {return a-B;} function NumDescSort (a, B) {return B-a;} var arr = new Array (1, 2, 3, 4); arr. sort (NumDescSort); alert (arr); // 4 3 2 1arr. sort (NumAscSort); alert (arr); // 1 2 3 4 Comparison character array test C: var myarray = ["Apple", "Banana", "Orange"] myarray. sort () alert (myarray); // After the Apple Banana Orange array directly calls sort (), the array sorts the elements in the array in alphabetical order, which is more accurate, sorts object arrays in the order of character encoding. We first write a function to construct a comparison function.

// The by function accepts a member name string as a parameter.
// Return a comparison function that can be used to sort the array of objects containing this member.
Var by = function (name)
{
Return function (o, p)
{
Var a, B;
If (typeof o = "object" & typeof p = "object" & o & p)
{
A = o [name];
B = p [name];
If (a = B) {return 0 ;}
If (typeof a === typeof B) {return a <B? -1: 1 ;}
Return typeof a <typeof B? -1: 1;
}
Else {throw ("error ");}
}
}
Var employees = []
Employees [0] = {name: "George", age: 32, retiredate: "March 12,201 4 "}
Employees [1] = {name: "Edward", age: 17, retiredate: "June 2, 2023 "}
Employees [2] = {name: "Christine", age: 58, retiredate: "December 20,203 6 "}
Employees [3] = {name: "Sarah", age: 62, retiredate: "Limit l 30,202 0 "}
Employees. sort (by ("age "));
Alert (employees );

By now, object array sorting is basically implemented. How can we sort multiple key values? That is, sort the age first. If the age is the same, compare the name.

In this case, we can further modify the by function so that it can accept the second parameter. When the primary key value produces a match, the other compare method will be called to determine the upper limit.

// The by function accepts a member name string and an optional secondary comparison function as the parameter.
// Return a comparison function that can be used to sort the object arrays of the member.
// When o [age] and p [age] are equal, the secondary comparison function is used to determine the upper limit.

Var by = function (name, minor)
{
Return function (o, p)
{
Var a, B;
If (typeof o = "object" & typeof p = "object" & o & p)
{
A = o [name];
B = p [name];
If (a = B) {return typeof minor = 'function '? Minor (o, p): 0 ;}
If (typeof a === typeof B) {return a <B? -1: 1 ;}
Return typeof a <typeof B? -1: 1;
}
Else {throw ("error ");}
}
}
Var employees = []
Employees [0] = {name: "George", age: 32, retiredate: "March 12,201 4 "}
Employees [1] = {name: "Edward", age: 17, retiredate: "June 2, 2023 "}
Employees [2] = {name: "Christine", age: 58, retiredate: "December 20,203 6 "}
Employees [3] = {name: "Sarah", age: 62, retiredate: "Limit l 30,202 0 "}
Employees. sort (by ('age', by ('name ')));
Alert (employees );

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.