1. Method Overview
Array's sort() method converts all elements into String by default and then sorts according to Unicode.
sort() will change the original array and return the changed (sorted) array.
2. Example
2.1
If no custom method is provided, the array element will be converted into a string and returns the sequence comparison results of the string under Unicode encoding.
var fruit = ['cherries', 'apples', 'bananas'];fruit.sort(); // ['apples', 'bananas', 'cherries']var scores = [1, 10, 2, 21]; scores.sort(); // [1, 10, 2, 21]// Watch out that 10 comes before 2,// because '10' comes before '2' in Unicode code point order.var things = ['word', 'Word', '1 Word', '2 Words']; things.sort(); // ['1 Word', '2 Words']; things.sort(); // ['1 Word', '2 Words', 'Word', 'word']// In Unicode, numbers come before upper case letters,// which come before lower case letters.
2.2 Using map to sort
// the array to be sortedvar list = ['Delta', 'alpha', 'CHARLIE', 'bravo'];// temporary array holds objects with position and sort-valuevar mapped = list.map(function(el, i) { return { index: i, value: el.toLowerCase() };})// sorting the mapped array containing the reduced valuesmapped.sort(function(a, b) { return +(a.value > b.value) || +(a.value === b.value) - 1;});// container for the resulting ordervar result = mapped.map(function(el){ return list[el.index];});alert(result);Reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
The above article "JS" is a comprehensive understanding of the sort() method that comes with you is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.