javascript - 如何按日期和另一个参数排序
问题描述
初始数组
var array = [
{
"name": "Service-1",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 10
},
{
"name": "Service-2",
"status": "Cancelled",
"startDate": "2020-04-03T00:00:00",
"id": 9
},
{
"name": "DG_3029_Export1",
"status": "Approved",
"startDate": "2020-03-01T00:00:00",
"id": 11
},
{
"name": "DG_3029_Export2",
"status": "Approved",
"startDate": "2020-04-02T00:00:00",
"id": 12
},
{
"name": "Service-10",
"status": "Cancelled",
"startDate": "2020-04-10T00:00:00",
"id": 19
}
];
预期结果:
var array = [
{
"name": "DG_3029_Export2",
"status": "Approved",
"startDate": "2020-04-02T00:00:00",
"id": 12
},
{
"name": "DG_3029_Export1",
"status": "Approved",
"startDate": "2020-03-01T00:00:00",
"id": 11
},
{
"name": "Service-10",
"status": "Cancelled",
"startDate": "2020-04-10T00:00:00",
"id": 19
},
{
"name": "Service-2",
"status": "Cancelled",
"startDate": "2020-04-03T00:00:00",
"id": 9
},
{
"name": "Service-1",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 10
}
];
按日期降序排序(这是我迄今为止能够实现的)
var array = [
{
"name": "Service-10",
"status": "Cancelled",
"startDate": "2020-04-10T00:00:00",
"id": 19
},
{
"name": "Service-2",
"status": "Cancelled",
"startDate": "2020-04-03T00:00:00",
"id": 9
},
{
"name": "Service-1",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 10
},
{
"name": "DG_3029_Export2",
"status": "Approved",
"startDate": "2020-04-02T00:00:00",
"id": 12
},
{
"name": "DG_3029_Export1",
"status": "Approved",
"startDate": "2020-03-01T00:00:00",
"id": 11
}
];
小提琴: https ://jsfiddle.net/fierce_trailblazer/gs9ek0oz/
我的目标是按日期排序,然后在开始时显示所有状态为“已批准”的对象
是否可以使用 javascript 排序按日期和附加参数排序?
var array = [{
"name": "Service-1",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 10
},
{
"name": "DG_3029_Export1",
"status": "Approved",
"startDate": "2020-03-01T00:00:00",
"id": 11
},
{
"name": "DG_3029_Export2",
"status": "Approved",
"startDate": "2020-04-02T00:00:00",
"id": 12
},
{
"name": "Service-10",
"status": "Cancelled",
"startDate": "2020-04-10T00:00:00",
"id": 19
},
{
"name": "DG_Export3",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 13
},
{
"name": "Service-5",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 14
},
{
"name": "Service-6",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 15
},
{
"name": "Service-7",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 16
},
{
"name": "DG_Export4",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 17
},
{
"name": "DG_Export5",
"status": "Cancelled",
"startDate": "2020-04-02T00:00:00",
"id": 18
}
];
array.sort((a, b) =>
new Date(b.startDate) - new Date(a.startDate)
)
for (let i = 0; i < array.length; i++) {
console.log("Id:\t" + array[i].id + "\tStatus:\t" + array[i].status + "\tDate:\t" + array[i].startDate.substring(1, 10));
}
解决方案
您需要先按状态排序,然后按日期排序,才能将所有已批准的项目放在首位。
var array = [{ name: "Service-1", status: "Cancelled", startDate: "2020-04-02T00:00:00", id: 10 }, { name: "Service-2", status: "Cancelled", startDate: "2020-04-03T00:00:00", id: 9 }, { name: "DG_3029_Export1", status: "Approved", startDate: "2020-03-01T00:00:00", id: 11 }, { name: "DG_3029_Export2", status: "Approved", startDate: "2020-04-02T00:00:00", id: 12 }, { name: "Service-10", status: "Cancelled", startDate: "2020-04-10T00:00:00", id: 19 }];
array.sort((a, b) =>
(b.status === "Approved") - (a.status === "Approved") ||
b.startDate.localeCompare(a.startDate)
);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- android - 地理位置到 Android 上的 webview
- regex - Chrome 开发人员使用正则表达式搜索文件
- databricks - 我们可以将现有的 AWS EC2 实例与 Databricks 一起使用吗?
- java - 条件映射 ((condition) ? get-this : get-that) 与 Org.ModelMapper
- amazon-dynamodb - 插入删除、dynamoDb 并检查 SpringBoot
- python - 使用 Python 查询页面对象模型
- asynchronous - 等到 openlayers 5 geolocation 返回一个值
- php - 有人可以向我解释一下这个 php 数组中的“\”是做什么的吗?
- json - 使用 powershell 正确格式化 json dict 对象
- c# - ASP.net mvc API 获取请求和 JSON 数据