在js中,想要使用sort方法根据集合中某字段给集合重新排序
- 数字排序
先准备一个排序函数:(不用改,直接用)
compare(property) { //适用于数字排序
return function(a, b) {
var value1 = a[property]
var value2 = b[property]
return value1 - value2
}
}
然后排序:
response.sort(this.compare('ShiftCode'))
//response 是待排序集合
//compare 是排序函数名
//ShiftCode 是集合中字段,根据此字段排序
- 中文排序
先准备一个排序函数(不用改,直接用)
// 根据班次编码排序,夜早中
// ShiftName 是集合中的字段,根据此字段排序
compare(item1, item2) {
return item1['ShiftName'].localeCompare(item2['ShiftName'])
},
然后排序
response = response.sort(this.compare)
//response 是待排序集合
//compare 是排序函数名