The Slice and sort methods of array in JS (converted from javaeye)

Source: Internet
Author: User

I. array. Slice (START, end) method:

Slice () is used to take part of an array and use it to copy the array. If the end parameter is omitted, the split array contains all elements starting from start to the end of the array.

Use it to copy the array, just one line:

VaR newarray = oldarray. Slice (0 );

Other Instructions:

1. If start is negative, it is processed as Length + start. Here, length is the length of the array, such as A. Slice (-), which is equivalent to a. Slice ).

2. If end is negative, it is processed as Length + end. Here, length is the length of the array, such as A. Slice (0,-1 ).

3. If end is omitted, the slice method will always be copied to the end of the source array, for example, A. Slice (1 ). If end appears before start, no elements are copied to the new array, such as A. Slice ).

4. For strings, the slice (START, end) method is also available.

II,Array. Sort () method:

The default sort () is sorted by character encoding:

Code1:

VaR testarray = [3,324,534, 665,];
Testarray. Sort ();
Alert (testarray );

The output result is:134 3 324 5345 5654 6546 665


Sort by size:

VaR testarray = [3,324,534, 665,];
Testarray. Sort (function (a, B) {return a-B ;});
Alert (testarray );

Here, a comparison function is passed Sort , The logic of the comparison function is: if the difference between the two parameters is less 0 , Indicates A Must appear in B Otherwise B . The output result is:

The output result is:3 134 324 665 5345 5654 6546,


Supplement: Sorting multidimensional arrays:

var arr = [
[,],
[,],
, ],
[,],
];
alert ("by default, the first row \ n" + arr. sort (). join ("\ n")
alert ("column \ n" + arr. sort (function (left, right) {return left [2]> right [2]? 1:-1 }). join ("\ n")
alert ("Inverted \ n" + arr. sort (function (left, right) {return left [2]> right [2]? -1:1}). Join ("\ n")

// Description: Alert (ARR. Sort (function (left, right) {return left> right? }))
// Here, the sort method uses the return value 1 or-1 of the Parameter Function to determine whether to forward or reverse

 

 

 

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.