1. 方法概述
Array的sort()方法默认把所有元素先转换为String再根据Unicode排序,
sort()会改变原数组,并返回改变(排序)后的数组 。
2. 例子
2.1
如果没有提供自定义的方法, 数组元素会被转换成字符串,并返回字符串在Unicode编码下的顺序比较结果
2.2 利用map来排序
// 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);参考 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
以上这篇js 自带的sort() 方法全面了解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。