The sort method of an array sorts the elements of the array and returns the sorted array. ⚠️ changes the array.

Syntax: array.sort (compareFunction), compareFunction specifies the sorting algorithm for itself. If no sorting algorithm is specified, the default sorting algorithm is used.

The default sorting algorithm for this method is to convert elements to strings and then to UTF-16 codes, sorting the elements according to the order in which they are utF-16 codes.

1. Default sort

1. String array sort

const months = ['March'.'Jan'.'Feb'.'Dec'];
months.sort();// [ 'Dec', 'Feb', 'Jan', 'March' ]

const txts = ['ability'.'absent'.'April'.'divide'.'center'.'context'.'container'];
txts.sort();// ['April', 'ability', 'absent', 'center', 'container', 'context', 'divide']
Copy the code

You can see that by default, arrays of strings are sorted by comparing letters one by one in UTF-16 encoding order.

2. Sort numeric arrays

const array = [1.30.4.21.10000.81.111.999];
array.sort();// [1, 10000, 111, 21, 30, 4, 81, 999]
Copy the code

There seems to be something wrong with 🤣…

Since there is no sorting algorithm specified, it is the default, so here it is sorting the array into strings, just like the string sorting above.

Second, specify the sorting algorithm

Array.sort(compareFunction) returns two values, assuming a and b:

  • compareFunction(a, b)The < 0,aWill be lined up tobIn front of.
  • compareFunction(a, b)Lambda is equal to 0, the relative positions of the two do not change.
  • compareFunction(a, b)> 0,aWill be lined up tobThe back of the.

Let’s look at an algorithm that can sort an array of numbers in ascending order:

const array = [1.30.4.21.10000.81.111.999];
console.log(array.sort((a, b) = > {
  return a - b;// descending b-a
}));// [1, 4, 21, 30, 81, 111, 999, 10000]
Copy the code

Understand it this way:

If b is the current traversal element in the array, and a is the previous traversal element adjacent to it, the rule is a-b, a-b > 0 means A > B, then A will be placed after B, and vice versa.

This can also sort string arrays:

const txts = ['ability'.'absent'.'April'.'divide'.'center'.'context'.'container'];
console.log(txts.sort((a, b) = > {
  return a - b;
}));
// ['April', 'ability', 'absent', 'center', 'container', 'context', 'divide']
Copy the code

Sort the array of objects

There is an array like this:

const members = [
  { name: 'Dave'.age: 32 },
  { name: 'Jeff'.age: 58 },
  { name: 'Mona'.age: 15 },
  { name: 'Ma'.age: 45 },
  { name: 'Lord'.age: 79}]Copy the code

To sort by the age attribute of each element in the array, you can:

console.log(members.sort((a, b) = > {
  return a.age - b.age;
}));
/ / /
// { name: 'Mona', age: 15 },
// { name: 'Dave', age: 32 },
// { name: 'Ma', age: 45 },
// { name: 'Jeff', age: 58 },
// { name: 'Lord', age: 79 }
// ]
Copy the code

If you want to sort by the name attribute, you can:

console.log(members.sort((a, b) = > {
  const a_name = a.name.toUpperCase();
  const b_name = b.name.toUpperCase();

  if (a_name > b_name) {
    return 1;
  } else if (a_name < b_name) {
    return - 1;
  } else {
    return 0; }}));/ / /
// { name: 'Dave', age: 32 },
// { name: 'Jeff', age: 58 },
// { name: 'Lord', age: 79 },
// { name: 'Ma', age: 45 },
// { name: 'Mona', age: 15 }
// ]
Copy the code

Chinese character array and mixed array sort

For pure Chinese characters or mixed arrays, the test is as follows:

const chnArray = ['you'.'我'.'han'.'word'.'number'.'group'.'to'.'than'];
const mixArray = ['you'.'me'.'han'.'word'.'num'.'array'.'to'.'than'];
const mixArray2 = ['you'.'me'.100.'han'.1.'word'.'num'.'array'.'to'.'than'.29];
Copy the code

Using sort() directly yields the following result:

The e comparison of Chinese characters is not known according to what sort, should also be UniCode bar.

Comparison of non-English strings

Chinese characters also belong to one of these strings, which can be compared with string.localecompare (compareString).

⚠️ Note that this method only works with strings.

console.log(chnArray.sort((a, b) = > {
  return a.localeCompare(b);
}));
// [' you ', 'word ',' right ', 'me ',' number ', 'bi ',' han ', 'group']
Copy the code