Previous article (array. from) : juejin.cn/post/686967…

Basic grammar

The _sort() method is used to sort the elements of an array.

Grammar: array sort (sortfunction)

Parameters:

parameter describe
sortfunction
Optional. Specify the sort order. It has to be a function.

The return value:

Type describe
Array Reference to an array. Note that arrays are sorted on top of the original array and no copies are made.

Sample usage

1. Don’t preach to participate

Let arr1 = (3, 2, 1), Arr1.sort () // [1, 2, 3] let arr2=[30,20,100]; arr2.sort() // [100, 20, 30] ???Copy the code

Strange not? 100 comes before 20. This is not because there is an error, but because sort gives you a default argument when you pass no arguments. The default comparefn method converts the contents of an array to a string (line 720,721 below).

In JS, the string type compares the character encoding values of two strings in sequence. Strings “100” and “20” compare “1” and “2” first, 1 is less than 2, so the result is that the string “100” is less than the string “20”. This reminds me of a question a friend asked me last year. He encountered a bug that seemed to have no errors when he looked at the code alone. This bug caused him to have an accident like 10<9 when calculating the number of coupons, but he could not find the problem at all. This was my first reaction, so I sent him the test code after testing the string comparison. After his debugger, he found that it was a data type problem. There’s a lot I’d like to say about relational operators, but I’m not going to expand on arrays.

2. The refs

Sortfunction sortfunction rule:

  1. Pass two parameters

  2. When the return value is positive, the positions of the two parameters in the array are swapped

    ,3,2,4 let testArr = [1] testArr. Sort ((a, b) = > {the console. The log (a, b)}) / / 1 / / 2/3 3/4 2

Here we see two arguments, the previous argument a actually passes in the last item in the array, contrary to our intuition. Very strange, then let’s take a look at v8 array source

// This is part of V8's Sort core, which is quick sorting. However, the following insertion sort method is called when the fragment length is quicksorted (to-form<10). Var InsertionSort = function InsertionSort(a, from,to) {//a: InsertionSort, 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; }}Copy the code

It doesn’t seem to be a problem.

/ / rules test let arr =,3,2,4 [1] arr. Sort ((a, b) = > 0) / / [1, 3, 2, 4] constant arr. Sort ((a, b) = > false) / / [1, 3, 2, Arr. Sort ((a,b)=>1) // [1, 3, 2, 4] arr. Sort ((a,b)=>-1) // Sort definition and source code are not the same as each website. This is going to be the opposite of the definition plus the argument a, b is going to be the opposite of the definition. A negative makes a positive, which makes it normal. Magic! Test for a long time to determine that I did not make a mistake, I hope to know the students can comment to me. Thank you! / / common way let testArr = testArr,3,2,4 [1]. The sort (a, b) = > (a - b) / / [1, 2, 3, 4] ascending testArr. Sort ((a, b) = > b - a) / / [4, 3, 2, 1] descending / / an array of objects let objArr = [{age: 25, name: 'LGC}, {age: 18, name:' loli}, {age 35, name: 'uncle'}] objArr. Sort ((a, b) = > a.a ge - b.a ge)  // [{age:18,name:'loli'}, // {age:25,name:'lgc'}, // {age:35,name:'uncle'}]Copy the code

Summary and points to note:

1. The sort method alters the original array

2. If no parameter is passed, the array items are converted to strings for comparison

3. Each time the sorting function passes two items in an array to compare and returns a positive (doubtful) value, the two items are swapped. All else being the same.