Reordering of arrays of JavaScript basics

Source: Internet
Author: User
Tags javascript array

Reordering of arrays of JavaScript basics

Array is the most commonly used type following object. Unlike other languages, each item of a JavaScript array can hold any type of data, and the size of the array can be dynamically adjusted.

JavaScript has provided us with two methods for reordering arrays, the reverse () and the sort () methods.

where the reverse () method reverses the order of the array items. Give me a chestnut:

var values = [18,4,56,1, "A", "B", "ABC"];values.reverse (); alert (values); abc,b,a,1,56,4,18

This example visually shows the effect of the reverse () method, just turning the head of the array into the tail, the tail programming head, as if the train were to turn around in the station. But without achieving the results we want, the sort () method works.

var values = [18,4,56,1, "A", "B", "ABC"];values.sort (); alert (values); 1,18,4,56,a,abc,b

We found that after using the sort () method, we really scrambled the position of the elements in the array, but not the large-to-small permutations we wanted to achieve. The original sort () method invokes the ToString () method of each array element and then compares the resulting string, resulting in the above result. But how do you do it from small to large or from big to small? So the sort () method can accept a comparison function as a parameter so that we specify which value is in front of which value.

The comparison function accepts two parameters, if the first argument should precede the second argument, returns a negative number if the first argument equals the second argument returns 0 if the second argument should precede the first one to return a positive number. Here is an example of a comparison function:

function Compare (value1, value2) {if (value1<value2) {return-1;    }else if (value1>value2) {return 1;    }else {return 0; }}

This comparison function can be applied to most data types, just pass the compare as a parameter to the sort () method, and give a chestnut as follows:

var values = [18,4,56,1, "A", "B", "ABC"];values.sort (comapre); alert (values); 1,4,18,56,a,abc,b

We find that the results of the output are arranged in ascending order, and if we want to arrange in a descending way, we just need to change the return value of the Compare function.

function Compare (value1, value2) {if (value1<value2) {return 1;    }else if (value1>value2) {return-1;    }else {return 0;        }}var values = [18,4,56,1, "A", "B", "ABC"];values.sort (Compare); alert (values); 56,18,4,1,b,abc,a

We found that the three judgments of the compare function are about the same size as we did when we were in primary school and we knew that comparing the two-digit size with the subtraction would be okay, so let's try to get the compare function to return directly to Value2-value1:

function Compare (value1, value2) {return value2-value1;}        var values = [18,4,56,1, "A", "B", "ABC"];values.sort (Compare); alert (values); 56,18,4,1,a,b,abc

Eh, how can only numbers be arranged in a large-to-small way, and the order of the strings does not change at all. Since it is subtraction, it must be valid only for values of type number.

So when we reorder arrays of pure number types, we can use the shorthand method to return value2-value1 from large to small, and return to Value1-value2 , and for a mix of multiple data types, it's an honest way to use the above method.

This article is from the "Fcheng" blog, make sure to keep this source http://fcheng.blog.51cto.com/10644114/1838024

Reordering of arrays of JavaScript basics

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.