How to sort Javascript arrays _ basic knowledge-js tutorial

Source: Internet
Author: User
JavaScript sorts multi-dimensional arrays and object arrays. It uses the native sort () method to sort array elements. Today we will discuss in detail the sort () method. If you have been using javascript for a while, you must know that the array sorting function sort is a method in the array prototype, that is, array. prototype. sort (), sort (compareFunction), where compareFunction is a comparison function. Let's look at a description from Mozilla MDN:
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. for example, "80" comes before "9" in lexicographic order, but in a numeric sort 9 comes before 80.

Here are some simple examples:

The Code is 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 shown in the preceding example, It is sorted by letters in the dictionary by default.

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

The Code is 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 another question: how can we control ascending and descending order?

The Code is 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 comparFunction sorting rules are as follows:
1. If it returns a negative number, a will be sorted to a lower index in the array.
2. If it returns a positive number, a will be sorted to a higher index.
3. And if it returns 0 no sorting is necessary.

Let's take a look at a passage from 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 method works on all platforms for JavaScript 1.2.

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array. For details, click here.

The Code is 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 will help you learn and understand the sort () method, and I hope you will criticize the sort () method.

Reference: 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.