In-depth exploration of the array sorting functions sort () and reverse () in JS ()

Source: Internet
Author: User

Recently, some of the differences have been discovered in Javascript research. It is a bit speechless, for example:

Alert (typeof (NaN = NaN); // The result is false.

Alert (typeof (NaN! = NaN); // The result is true.

Hey, of course, this article is not for discussion !! Start our text

First, let's take a look at the functions of the sort () and reverse () functions in JS.

Reverse (); this function is used for reverse order. There is nothing to say about this. The so-called reverse order is the big one at the front and the small one at the back.

For example:

Var array1 = [0, 1, 5, 10, 15];

Array1.reverse (); // The result is: 15, 10, 5, 1, 0.

Sort ([parameter]); this function is used to sort arrays in the forward order. However, I found that, it seems that we use this function in the same way in most languages !! However, you can see that this function actually has parameters.

Anyway, when we were studying the JS course, the teacher did not explain that this function had this parameter.

As I have already said, it makes a difference to the forward-order function of other languages. Let's give you an example.

Var array1 = [0, 1, 5, 10, 15];

Array1.sort (); // The result is: 0, 1, 10, 15, 5.

You can try to see if this result is correct !! I don't know why when I see the result. Is this function not in the forward order, so I went to the Internet to find relevant information. In W3CSchool, I can see some descriptions of this function. For example



I use the red line, which is the root cause of the influence of this function in sorting. (In fact, when sort () is used for sorting, The toString () function is called to convert the value to a string for comparison, which is compared by ASCII)

It is easy to understand: the ASCII value of 1 must be smaller than the ASCII value of 5, because the smaller the positive order, the more at the front !! So what can we do to make it sort correctly to achieve the desired effect. At this time, the parameter in sort () will be used. This parameter points to a function! I believe that when you see the above figure, I will not explain it too much. Let's take a look at the following code. I am analyzing the implementation process.



Note: The sort () method can accept a method as a parameter. This method has two parameters. Represent the two array items for each sort comparison. This parameter is executed every time two array items are compared during sort () sorting, and two compared array items are passed as parameters to this function. When the return value of the function is 1, the order of two array items is exchanged. Otherwise, the order is not exchanged.

First, let's explain it! The sorting algorithm is inseparable from the bubble algorithm! The bubble algorithm is also used here, but how can we understand this computing definition function!

This computation definition function must have two parameters (this is sorting, in other words, it is before who is big !! Is to compare the size .)

The function means that when v1 V2 is 1, v1 = v2 is 0

Based on the bubble algorithm: pass two values to v1 and v2, array [0] = "0", array [1] = "2", then v1 = 0, v2 = 2,

First round: according to the above rules, the first round is: 0.5, 1, because 0 is smaller than any number, so the first round ends, he is always at the beginning, so I will not analyze it.

Second round: v1 = 2, v2 = 1;

First time: Because v1> v2, the positions of v1 and v2 values are: 0, 1, 2, 0.5;

The second time: v1 = 1, v2 = 2. The comparison result is: 0, 1, 2, 0.5,

Third time: At this time, v1 = 1, v2 = 0.5 because v1> v2, v2 and v2 are switched. The result is: 0, 0.5, 1, 2.

The fourth time: v1 = 0.5, v2 = 2. The result is: 0, 0.5, 1, 2.

Round 3: v1 = 1, v2 = 2;

For the first time: The result of the comparison is 0, 0.5, and. This is because it has reached the end of the array. Exit the function. Returned results.

Well, this is a discussion of the sort () function. I hope you will have a deep understanding of the JS sorting function after reading this article. Of course, I have explained this method as a general method. You can also write it according to your own needs, such as descending order, large and lowercase letters... I will not talk about it here !! There may be some places where the explanation is not good enough. I hope you will understand. If you have any questions, please leave a message below the article.

Original tutorial on Starting Point Network

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