Measure the test taker's knowledge about the method of reordering javascript arrays.

Source: Internet
Author: User
This article describes in detail how to reorder arrays in js and explains them one by one. Native reordering of javascript Arrays

1. array reverse Method

(1) Role

Reversing the element position in the array (changing the original array)

(2) syntax

Arr. reverse () // No Parameter

(3) Example

let myArray =  [ 'one', 'two', 'three' ]; myArray.reverse();console.log(myArray); //  ["three", "two", "one"]

(4) Return Value
The reference of the original array will not be generated as an array

let myArray =  [ 'one', 'two', 'three' ]; let result = myArray.reverse();result[2] = 2;console.log(myArray);   //["three", "two", 2]console.log(result);    //["three", "two", 2]

(5) Disadvantages
Not flexible. Only Arrays can be reversed.

2. array sort method

(1) Role
Sort () sorts the elements of the array at the appropriate position (the original array will change)

(2) syntax

arr.sort();arr.sort(compareFunction);

(3) Parameters
Optional. A function used to sort data in a certain order.

If the parameter is omitted, such as this call.

Arr. sort () // No parameter is added

The following parameters are applied to the sort () function, so the Unicode code is compared by default.

// Optional sort parameters // if no parameters are set, a parameter if (! IS_CALLABLE (comparefn) {// This is the default parameter value comparefn = function (x, y) {if (x = y) return 0; if (% _ IsSmi (x) & % _ IsSmi (y) {return % SmiLexicographicCompare (x, y );} // here all parameters are converted to Unicode for comparison x = TO_STRING (x); y = TO_STRING (y); if (x = y) return 0; else return x <y? -1: 1 ;};}

(4) Principle
1. Set parameters first, and enter or use the default value.

2. If the number of elements is less than 2, return directly

3. Check whether the current object is an array. if the object is not an array, the value of the prototype chain is copied to the current array for sorting.

    var arry =  [1,2,3,4,2,5];    var a = {};    a.proto =  arry;    a.sort() //  Array {0: 1, 1: 2, 2: 2, 3: 3, 4: 4, 5: 4}

4. Sort the undefined values in the array at the end,

5. All undefind values in the SORT Array

6. When the number of elements in the array is less than or equal to 10, use InsertionSort to sort

// Comparefn is the sort parameter. // You can input the parameter by yourself, or use the default function InsertionSort (a, from, to) {for (var I = from + 1; I <to; I ++) {var element = a [I]; for (var j = I-1; j> = from; j --) {var tmp = a [j]; var order = comparefn (tmp, element); if (order> 0) {a [j + 1] = tmp ;} else {break ;}} a [j + 1] = element ;}};

7. When the number of elements in the array is greater than 10, use quick sorting.

The above is a detailed introduction to the method of reordering javascript arrays. For more information, see other related articles in the first PHP community!

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.