Happy Mid-Autumn Festival, fly yourself

Sort ()

The default sort order is built when converting elements to strings and then comparing their UTF-16 code unit value sequences

        const arr = ['76wenhu'.'wutong'.'taiwansheng'];
        console.log(arr.sort());
        //0: "76wenhu"
        // 1: "taiwansheng"
        // 2: "wutong"
Copy the code

Special points in sort()

        const array1 = [1.30.4.21.100000];
        array1.sort();
        console.log(array1);
        // This is the result of the comparison by the sort of string, 100000 appears before 4.
        // expected output: Array [1, 100000, 21, 30, 4]
Copy the code

If compareFunction is not specified, elements are sorted by the Unicode loci of the characters converted to the string. For example, “Banana” will be placed before “cherry”. When numbers are sorted from smallest to largest, 9 appears before 80, but because (no compareFunction is specified) the comparison number is converted to a string first, “80” precedes “9” in Unicode order. If compareFunction is specified, the array is sorted by the value returned from calling the function. That is, a and B are the two elements to be compared:

  • If compareFunction(a, b) is less than 0, then A is placed before B;

  • If compareFunction(a, b) is equal to 0, the relative positions of a and B remain the same. Note: The ECMAScript standard does not guarantee this behavior, and not all browsers comply (Mozilla prior to 2003, for example);

  • If compareFunction(a, b) is greater than 0, b will be placed before A.

  • CompareFunction (a, b) must always return the same comparison result on the same input, otherwise the sorting result will be indeterminate.

This is the logic at the heart of sort

function compare(a, b) {
  if (a < b ) {           // By some sort of comparison, a is less than B
    return -1;
  }
  if (a > b ) {
    return 1;
  }
  // a must be equal to b
  return 0;
}
Copy the code

I think sort is very important for sorting objects

Objects can be sorted by an attribute:var items = [
  { name: 'Edward'.value: 21 },
  { name: 'Sharpe'.value: 37 },
  { name: 'And'.value: 45 },
  { name: 'The'.value: -12 },
  { name: 'Magnetic' },
  { name: 'Zeros'.value: 37}];// sort by value
items.sort(function (a, b) {
  return (a.value - b.value)
});

// sort by name
items.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});

Copy the code

Mapping sorting is also important

Use maps to improve sorting

Comparefunctions may need to map elements multiple times to achieve sorting, especially if comparefunctions are complex and have many elements, some comparefunctions can be very heavy. It would be a good idea to use Map-assisted sorting. The basic idea is to first extract the actual value for each element in the array, sort it, and then restore the array.

// The array to be sorted
var list = ['Delta'.'alpha'.'CHARLIE'.'bravo'];

// Temporary storage of numbers and positions to be sorted
var mapped = list.map(function(el, i) {
  return { index: i, value: el.toLowerCase() };
})

// Sort the array by multiple values
mapped.sort(function(a, b) {
  return +(a.value > b.value) || +(a.value === b.value) - 1;
});

// Get the sort result by index
var result = mapped.map(function(el){
  return list[el.index];
});
Copy the code