【发布时间】:2021-01-14 01:48:37
【问题描述】:
我有一组对象,我想先按日期排序,即 desc 中的“create_date_format”,如果 tie 然后按字母顺序排序风险,即 asc
我试过 lodash.orderBy(risk_list, ['create_date_format', 'risk'], ['desc']) 但由于日期是字符串格式,它按日期字符串中的数字排序,因此例如 2020 年 1 月 28 日出现在 01 之前2020 年 8 月,因为 28 比 01 高。
let arr = [{
avoided: 0,
avoided_note: null,
create_date_format: "28 Sep 2020",
id: 209,
notes: "Nothing is happening",
risk: "very high risk",
severity: 3,
severity_name: "High",
type: 1,
type_name: "Internal"
}, {
avoided: 0,
avoided_note: null,
create_date_format: "23 Sep 2020",
id: 206,
notes: null,
risk: "Risk 12",
severity: 3,
severity_name: "High",
type: 2,
type_name: "External"
}, {
avoided: 0,
avoided_note: null,
create_date_format: "22 Sep 2020",
id: 202,
notes: "test note",
risk: "test risk",
severity: 3,
severity_name: "High",
type: 2,
type_name: "External"
}, {
avoided: 0,
avoided_note: null,
create_date_format: "23 Sep 2020",
id: 206,
notes: null,
risk: "abc Risk 12",
severity: 3,
severity_name: "High",
type: 2,
type_name: "External"
}]
我想首先按日期排序,即 desc 中的“create_date_format”,如果并列,则按字母顺序在 asc 中的“风险”上排序
【问题讨论】:
-
你不需要像lodash这样的外部库;你所需要的只是
Array.prototype.sort()(developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…),如果你想根据两个条件进行排序,你可以做类似arr.sort((a, b) => return [sorting condition 1] || [sorting condition 2])
标签: javascript node.js arrays reactjs sorting