需求场景:接收到格式如下数据
{
"data":{
"items": [
{ "beRecommendName":"John2" , "beRecommendCityName":"北京" ,"recommendTimeString":"2017.08.22 15:00","rewardPrice":"100","recStatus":"1","id":"2"},
{ "beRecommendName":"John3" , "beRecommendCityName":"上海" ,"recommendTimeString":"2017.08.22 15:00","rewardPrice":"200","recStatus":"2"},
{ "beRecommendName":"John4" , "beRecommendCityName":"广州" ,"recommendTimeString":"2017.08.22 15:00","rewardPrice":"100","recStatus":"3"},
{ "beRecommendName":"John5" , "beRecommendCityName":"上海" ,"recommendTimeString":"2017.08.22 15:00","rewardPrice":"300","recStatus":"4"}
]
}
}
其中recStatus是用来判断用户的状态是等待申请,已经领取,正在审批,审批失败这四种状态,要求在用户执行操作后在状态变更后不同状态的显示值按照等待申请,已经领取,正在审批,审批失败的排序排列
<script>
var items = [
{
"beRecommendName": "John1",
"beRecommendCityName": "上海",
"recommendTimeString": "2017.08.22 15:00",
"rewardPrice": "100",
"recStatus": "1",
"id": "1"
},
{
"beRecommendName": "John2",
"beRecommendCityName": "北京",
"recommendTimeString": "2017.08.22 15:00",
"rewardPrice": "100",
"recStatus": "1",
"id": "2"
},
{
"beRecommendName": "John3",
"beRecommendCityName": "上海",
"recommendTimeString": "2017.08.22 15:00",
"rewardPrice": "200",
"recStatus": "2"
},
{
"beRecommendName": "John4",
"beRecommendCityName": "广州",
"recommendTimeString": "2017.08.22 15:00",
"rewardPrice": "100",
"recStatus": "3"
},
{
"beRecommendName": "John5",
"beRecommendCityName": "上海",
"recommendTimeString": "2017.08.22 15:00",
"rewardPrice": "300",
"recStatus": "4"
}
]
var info = items.sort(function (recStatus) {
return function (a, b) {
var a = a[recStatus],
b = b[recStatus];
return a - b;
}
})
console.log(info);
</script>
这里注意一点,最好的对数据进行排序的方式是由后台进行排序,前端只负责展示,因为前端排序相比于后端排序更加耗费性能