Delve into the array sort functions in JS sort () and reverse ()

Source: Internet
Author: User
Keywords In-depth

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

The recent study of JavaScript found some of the more supernatural things. It's a little bit of a feeling of silent.

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

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

Hey, of course this is not this article to discuss!! Start our text

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

Reverse (); This function is used to reverse, this has nothing to say, the so-called reverse is the big in front, small in the back.

Like what:

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

Array1.reverse ()///Result: 15,10,5,1,0

Sort ([parameters]); This function is used to arrange the array in a positive order, but I found that everyone seems to be more conventional to use this function for most languages!! But, yes, you've found that the function is actually parametric.

Anyway, when we learn JS This course, the teacher is not explained that this function has this parameter.

As I have just said, it gives us a distinction between the positive and orderly functions of other languages, for example.

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

Array1.sort ()///Result: 0,1,10,15,5

We can try to see if this is the result!! When I see the result, I don't know why. This function is not to do a positive order, so go to the Internet to find relevant information, in the W3cschool see, this function of some instructions. The following figure

  

We read the explanations in the diagram above, where I use the red part, which is the root cause of the effect of this function on the ordering process. (In fact, the ToString () function is invoked to convert its value to a string for comparison, compared by ASCII) when sorting using sort ().

It's not hard to understand: 1 of the ASCII value must be smaller than the ASCII value of 5, because it is a positive sequence, the smaller the more in front!! What are we going to do to get it sorted to the desired effect? This time, you will use the parameters in sort (). This parameter points to a function! I believe that when you see the picture above, I am not doing too much explaining. Look, the following code, I'm analyzing, his implementation process.

  

Note: the sort () method can accept a method as a parameter, and this method has two parameters. Represents each of the two array entries for each sort comparison. Sort () performs this argument each time the comparison of two array entries is performed and passes the two comparison array items as arguments to the function. The order of two array items is exchanged when the function returns a value of 1, otherwise it is not exchanged.

First of all, to explain! The sorting algorithm is inseparable from the bubble algorithm! The bubble algorithm is also used here, but how do we understand the definition function?

This definition function must have two parameters (this is sorted, in other words, who is big who is in the front!!) is to compare size. )

function means when V1V2 is 1,v1==v2 0

According to bubble algorithm: pass two values to v1 and v2,array[0] = "0", array[1]= "2" Then, v1=0,v2=2,

The first round: according to the above rules, do not swap position, then the first time down is: 0,2,1,0.5 because 0 is smaller than any number so the first round is over, he is always at the front I don't analyze

Second round: v1=2,v2=1;

The first time: because V1>v2 so v1 and V2 value Exchange position result is: 0,1,2,0.5;

The second time: this time v1=1,v2=2 to compare the knot entitled: 0,1,2,0.5,

The third time: this time v1=1,v2=0.5 because V1>v2 so v2 and v2 exchange position, the result is: 0,0.5,1,2

Fourth time: This time v1=0.5,v2=2 is compared the result is: 0,0.5,1,2

Third round: v1=1,v2=2;

First time: Compare the result to 0,0.5,1,2 this time due to the end of the array. Exits the function. Returns the result.

OK, this is the sort () function of a discussion, hope that we read this article on the JS sort function has a more in-depth understanding. Of course, I explained this kind of writing is a more general style, we can also according to their own needs to write, reverse, large, lowercase and so on ... Here is not to say more!! There may be a few places that are not well explained. I hope you will understand. If you have any questions, please leave a message under the article.

Original Web Tutorial

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.