jquery 发送请求,获取一个json数组。按照数组中的日期字段做排序。
data的结构是酱紫滴:
[{
"id": "123456",
"title": "title1",
"releaseTime": "2020-05-10 16:00:00"
},
{
"id": "567890",
"title": "title2",
"releaseTime": "2020-05-11 16:00:00"
},
{
"id": "567890",
"title": "title2",
"releaseTime": "2020-05-11 16:00:00"
}
]
上代码:
try{
$.ajax({
type:"post",
dataType: 'json',
contentType: "application/json",
data:JSON.stringify({"userUid":user.uid,"noticeType":["other"]}),
url: o.url,
success: function(data) {
console.log(data.data);
console.log("length:"+data.data.length);
var desc = function(x,y)
{
// "releaseTime" 是日期
return (x["releaseTime"] < y["releaseTime"]) ? 1 : -1
}
var asc = function(x,y)
{
return (x["releaseTime"] > y["releaseTime"]) ? 1 : -1
}
// 调用sort方法后,data即做了排序,desc是降序,要升序排列的话就用asc
console.log(data.data.sort(desc));
}
});
}catch(e){}