Sub*_*Sul 1 javascript arrays sorting
我正在尝试使用三个级别的自定义排序标准对一组对象进行排序。
firstLevelSortOrder = ['10K','20K','30K','40K','50K']
secondLevelSortOrder = ['ACTIVE','CONSUMED','EXPIRED']
thirdLevelSortOrder =
1) Oldest bountyIssueDate in case of multiple ACTIVE objects
2) Latest bountyIssueDate in case of multiple CONSUMED objects
3) Latest bountyIssueDate in case multiple EXPIRED objects
Run Code Online (Sandbox Code Playgroud)
要排序的数组:
let bountyList = [
{
"bountyCode": "10K",
"bountyIssueDate": "13 Jan 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "20K",
"bountyIssueDate": "13 Feb 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "30K",
"bountyIssueDate": "13 Mar 2020",
"bountyStatus": "EXPIRED"
},
{
"bountyCode": "40K",
"bountyIssueDate": "13 Apr 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "50K",
"bountyIssueDate": "13 May 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "10K",
"bountyIssueDate": "29 May 2020",
"bountyStatus": "ACTIVE"
},
{
"bountyCode": "10K",
"bountyIssueDate": "06 Jun 2020",
"bountyStatus": "ACTIVE"
},
{
"bountyCode": "30K",
"bountyIssueDate": "15 Mar 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "40K",
"bountyIssueDate": "23 Apr 2020",
"bountyStatus": "CONSUMED"
}
]
Run Code Online (Sandbox Code Playgroud)
结果数组:
[
{
"bountyCode": "10K",
"bountyIssueDate": "29 May 2020",
"bountyStatus": "ACTIVE"
},
{
"bountyCode": "10K",
"bountyIssueDate": "06 Jun 2020",
"bountyStatus": "ACTIVE"
},
{
"bountyCode": "10K",
"bountyIssueDate": "13 Jan 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "20K",
"bountyIssueDate": "13 Feb 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "30K",
"bountyIssueDate": "15 Mar 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "30K",
"bountyIssueDate": "13 Mar 2020",
"bountyStatus": "EXPIRED"
},
{
"bountyCode": "40K",
"bountyIssueDate": "23 Apr 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "40K",
"bountyIssueDate": "03 Apr 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "50K",
"bountyIssueDate": "13 May 2020",
"bountyStatus": "CONSUMED"
}
]
Run Code Online (Sandbox Code Playgroud)
我已经完成了第一级排序工作,这在这个fiddle 中。试图整合二级和三级排序顺序时遇到了死胡同。还尝试将重复项分离到一个单独的数组中,对它们进行排序,然后将它们合并到一个主数组中,它变得非常混乱。
您需要根据按顺序检查每个排序条件来实现这一点。如果第一级排序标准给出结果,则返回该结果。否则,检查第二级。如果这给出了结果,则返回该结果。否则,返回第三级标准的结果:
var bountyList = [{
"bountyCode": "10K",
"bountyIssueDate": "13 Jan 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "20K",
"bountyIssueDate": "13 Feb 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "30K",
"bountyIssueDate": "13 Mar 2020",
"bountyStatus": "EXPIRED"
},
{
"bountyCode": "40K",
"bountyIssueDate": "13 Apr 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "50K",
"bountyIssueDate": "13 May 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "10K",
"bountyIssueDate": "29 May 2020",
"bountyStatus": "ACTIVE"
},
{
"bountyCode": "10K",
"bountyIssueDate": "06 Jun 2020",
"bountyStatus": "ACTIVE"
},
{
"bountyCode": "30K",
"bountyIssueDate": "15 Mar 2020",
"bountyStatus": "CONSUMED"
},
{
"bountyCode": "40K",
"bountyIssueDate": "23 Apr 2020",
"bountyStatus": "CONSUMED"
}
]
var firstLevelSortOrder = ['10K', '20K', '30K', '40K', '50K']
var secondLevelSortOrder = ['ACTIVE', 'CONSUMED', 'EXPIRED']
console.log(bountyList.sort(
function(a, b) {
var first = firstLevelSortOrder.indexOf(a.bountyCode) - firstLevelSortOrder.indexOf(b.bountyCode);
if (first != 0) {
return first;
}
// first order equal, test second order
var second = secondLevelSortOrder.indexOf(a.bountyStatus) - secondLevelSortOrder.indexOf(b.bountyStatus);
if (second != 0) {
return second;
}
// second order also equal, test dates dependent on bountyStatus
if (a.bountyStatus == 'ACTIVE') {
// want earliest date sorted first
return Date.parse(a.bountyIssueDate) - Date.parse(b.bountyIssueDate);
}
else {
// want latest date sorted first for CONSUMED or EXPIRED
return Date.parse(b.bountyIssueDate) - Date.parse(a.bountyIssueDate);
}
}
))Run Code Online (Sandbox Code Playgroud)
请注意,为了方便演示我用来Date.parse()解析对象中日期的代码。通常,由于浏览器不一致(请参阅手册),因此不建议这样做,为此您应该手动将日期解析为时间戳。