function

This method sorts the elements of an array and returns the sorted array, changing the original array.

use

Don’t preach to participate

The sort method is always sorted by the ASCII value of the first character when no argument is passed. The default sort order is ascending

const arr = [1, 2, 3, 4, 5, 6, 7, 11, 22, 33, 44]
const arr2 = ['a', 'd', 'c', 'e', 'b']

console.log(arr.sort()) // 1, 11, 2, 22, 3, 33, 4, 44, 5, 6, 7

console.log(arr2.sort()) // a, b, c, d, e
Copy the code

The ginseng

  • Take a function that takes two parameters a and b,
  • The first element that A compares, the second element that B compares,
  • You can specify a-b or B-a to ascending or descending the array
const arr = [5, 2, 4, 1, 6, 3]
arr.sort((a, b) => b - a)
console.log(arr) // 6, 5, 4, 3, 2, 1

arr.sort((a, b) => a - b)
console.log(arr) // 1, 2, 3, 4, 5, 6
Copy the code

The callback function controls the implementation of the sort method

Bubble sort

The internal sort idea of sort method is bubble sort idea. By comparing two adjacent numbers, the position is exchanged according to the size of the comparison. The core of bubble sort is the place where two variables are exchanged

// Bubble sort - const arr = [2, 3, 4, 5, 6, 7, 8, 1] i < arr.length; For (let j = 0; j < arr.length - 1 - i; If (arr[j] < arr[j + 1]) {if (arr[j] < arr[j + 1]) {if (arr[j] < arr[j + 1]) {if (arr[j] < arr[j]) {if (arr[j] < arr[j + 1]) {if (arr[j] < arr[j]) { Can also through the temporary variable exchange [arr [j], arr [m + 1]] = [arr [j + 1], arr [j]]}}} the console. The log (arr) / / 8, 7, 6, 5, 4, 3, 2, 1Copy the code

Performance optimization

In the above code, we have implemented sorting a set of numbers from largest to smallest. If we sort an array that has already been sorted from largest to smallest and then sort it in this way, the number of loops is the same. We need to reduce unnecessary loops

Const arr = [2, 3, 4, 5, 6, 7, 8, 1] for (let I = 0; i < arr.length; i++) { count ++ for (let j = 0; j < arr.length - 1 - i; j++) { count ++ if (arr[j] < arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], Arr [j]]}}} console.log(count) //Copy the code
Const arr = [8, 7, 6, 5, 4, 3, 2, 1] for (let I = 0; i < arr.length; i++) { count ++ for (let j = 0; j < arr.length - 1 - i; j++) { count ++ if (arr[j] < arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], Arr [j]]}}} console.log(count) //Copy the code

It can be found that regardless of the out-of-order or sorted, the number of cycles is 36. If a large group of data is sorted in this form, the performance will inevitably be reduced

Const arr = [8, 7, 6, 5, 4, 3, 2, 1] for (let I = 0; i < arr.length; I++) {isSort = true count ++ for (let j = 0; j < arr.length - 1 - i; If (arr[j] < arr[j + 1]) {if (arr[j] < arr[j + 1]) {if (arr[j] < arr[j + 1]) {if (arr[j] < arr[j]) { IsSort = false [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]]}} If (isSort) {break out of the loop}} console.log(count) // 8Copy the code

Through the optimized code, you can see that the number of loops has been reduced to eight because each outer loop is required

Encapsulation method – control ascending or descending order

Using a method or trying to encapsulate a method can be done as follows

  1. The functionality of this method
  2. Parameter definition and parameter type
  3. Whether there is a return value
/* *@param {Array} arr *@param {Function} fun *@return {Array} */ const _sort = (arr, fun) => { let isSort for (let i = 0; i < arr.length; i++) { isSort = true for (let j = 0; j < arr.length - 1 - i; If (fun(arr[j], arr[j + 1]) > 0) {isSort = false; [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]] } } if (isSort) { break } } return arr } const arr = [2, 3, 1, 4, 2, 6] console.log(_sort(arr, (a, b) => a - b)) // 1, 2, 2, 3, 4, 6 console.log(_sort(arr, (a, b) => b - a)) // 6, 4, 3, 2, 2, 1Copy the code