字母排序
2022-04-26 本文已影响0人
辉色星空下
一般接口返回的数据就如下面的一样,是乱序的,要进行排序,对字母进行比较,可以用到“localeCompare”,而排序,可以使用js的“sort”方法,那么就好办了
var brands = [
{"name": "D",},
{"name": "G",},
{"name": "A",},
{"name": "Z",},
{"name": "H",},
{"name": "K",},
{"name": "B",},
{"name": "C",},
]
brands.sort(function(a,b){
return a.name.localeCompare(b.name)
})
输出结果如下
[
{"name": "A"},
{"name": "B"},
{"name": "C"},
{"name": "D"},
{"name": "G"},
{"name": "H"},
{"name": "K"},
{"name": "Z"},
]