如果数组小于10是用sort()函数可以直接排序,eg:
let numberList = [1,2,8,3,5,6,7]
console.log(numberList.sort())// [1, 2, 3, 5, 6, 7, 8]
如果数组大于10可以使用sort()函数传参,eg:
let numberList = [1,2,28,3,5,6,7]
console.log(numberList.sort())// [1, 2, 28, 3, 5, 6, 7]
console.log(numberList.sort((a,b)=>a-b))// [1, 2, 3, 5, 6, 7, 28] 升序
console.log(numberList.sort((a,b)=>b-a))// [28, 7, 6, 5, 3, 2, 1] 降序
如果数组是中文可以用sort()下localeCompare()方法
let chinaList=['黄山','瀑布','安徽']
chinaList.sort((a,b)=>a.localeCompare(b,'zh-CN'))// ["安徽", "黄山", "瀑布"]