Javascript Array Sorting Detailed _ basics

Source: Internet
Author: User
Tags array sort javascript array

If you've been in touch with JavaScript for a while, you know the array sort function Sort,sort is a method in the array prototype, Array.prototype.sort (), sort (comparefunction), Where comparefunction is a comparison function, let's look at a description from the Mozilla MDN:
If Comparefunction is isn't supplied, elements are sorted by converting them to strings and comparing strings in LEXICOG Raphic ("dictionary" or "telephone book, ' not Numerical ' order.) For example, "80″comes before" 9″in lexicographic order, but in a numeric sort 9 comes before.

Let's look at some simple examples:

Copy Code code as follows:

Output [1, 2, 3]
Console.log ([3, 2, 1].sort ());

Output ["A", "B", "C"]
Console.log (["C", "B", "A"].sort ());

Output [1, 2, "a", "B"]
Console.log (["B", 2, "a", 1].sort ());


As you can see from the example above, the default is sorted by the order of the letters in the dictionary.

Luckily, sort accepts a custom comparison function, as in the following example:

Copy Code code as follows:

function Comparefunction (A, b) {
if (a > B) {
return-1;
}else if (a < b) {
return 1;
}else {
return 0;
}
}
Outputs ["ZUOJJ", "Benjamin", "1"]
Console.log (["Benjamin", "1", "ZUOJJ"].sort (comparefunction));

After sorting we have a question, how to control the ascending and descending?

Copy Code code as follows:

function comparefunction (flag) {
Flag = flag? Flag: "ASC";
return function (A, b) {
if (a > B) {
return flag = = "desc"? -1:1;
}else if (a < b) {
return flag = = "desc"? 1:-1;
}else {
return 0;
}
};
}
Outputs ["1", "Benjamin", "ZUOJJ"]
Console.log (["Benjamin", "1", "ZUOJJ"].sort (Comparefunction ()));
Outputs ["ZUOJJ", "Benjamin", "1"]
Console.log (["Benjamin", "1", "ZUOJJ"].sort (comparefunction ("desc"));

The collation of the comparfunction is this:
1.If it returns a negative number, a would be sorted to a lower index in the array.
2.If it returns a positive number, a would be sorted to a higher index.
3.And if it returns 0 no sorting is necessary.

Let's take a look at some of the words from the Mozilla MDN:
The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2. To explain this description, let's look at an example:

In JavaScript 1.1, on some platforms, the sort method does not work. This is platforms for JavaScript 1.2.

In JavaScript 1.2, this is no longer converts undefined elements to null; Instead it sorts them to the "high" end of the array. Please stamp here for details.

Copy Code code as follows:

var arr = [];
Arr[0] = "Ant";
ARR[5] = "Zebra";
Outputs ["Ant", 5: "Zebra"]
Console.log (arr);
Outputs 6
Console.log (arr.length);
Outputs "Ant*****zebra"
Console.log (Arr.join ("*"));
Sort
var Sortarr = Arr.sort ();
Outputs ["Ant", "Zebra"]
Console.log (Sortarr);
Outputs 6
Console.log (sortarr.length);
Outputs "ant*zebra****"
Console.log (Sortarr.join ("*"));

I hope this article is helpful for you to learn and understand the sort () method, and it is also expected to criticize treatise in the article.

Reference Link: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Related Article

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.