简体   繁体   English

按日期对 desc 进行排序,如果匹配则在 javascript 数组中进行风险排序

[英]Sort desc on date and if tie then on risk in javascript array

I've array of objects which i want to sort first on date ie, 'create_date_format' in desc & if tie then sort on risk alphabetically ie, asc我有我想先按日期排序的对象数组,即 desc 中的“create_date_format”,如果平局,则按字母顺序对风险进行排序,即 asc

I tried lodash.orderBy(risk_list, ['create_date_format', 'risk'], ['desc']) but as date is in string format it sorted on numbers in date string so for eg 28 jan 2020 appears before 01 aug 2020 as 28 is higher than 01.我试过 lodash.orderBy(risk_list, ['create_date_format', 'risk'], ['desc']) 但由于日期是字符串格式,它按日期字符串中的数字排序,因此例如 28 jan 2020 出现在 2020 年 8 月 1 日之前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"
}]

I want to sort first on date ie, 'create_date_format' in desc and if tie then on 'risk' alphabetically in asc我想先按日期排序,即 desc 中的“create_date_format”,如果打平,则按字母顺序按 asc 排序“风险”

let sorted = arr.sort((a, b) =>
  // sort by create_date_format desc
  (new Date(b.create_date_format) - new Date(a.create_date_format)) 
  // if 0 (tie), sort by risk asc
  || a.risk.localeCompare(b.risk) 
)

What you can do is write a sort function that has a simple check for comparing properties.您可以做的是编写一个排序函数,该函数具有用于比较属性的简单检查。 In order to sort by date first, you'll need to convert the string date into a Date instance in order to sort the values by time as opposed to by char.为了首先按日期排序,您需要将字符串日期转换为Date实例,以便按时间而不是按字符对值进行排序。

After you create a date instance for the two objects you're comparing, you can check if the two date items are equal.为要比较的两个对象创建日期实例后,您可以检查两个日期项是否相等。 If they are, then skip this sort logic and execute a sort based on risk .如果是,则跳过此排序逻辑并根据risk执行排序。 See the snippet看片段

 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"}] var sortedArr = arr.sort((a, b)=>{ var aDate = new Date(a.create_date_format) var bDate = new Date(b.create_date_format) if (aDate.getTime() === bDate.getTime()){ //for strings return 1 or -1 depending on condition return b.risk > a.risk ? 1 : -1 } else { return aDate - bDate } }) console.log(sortedArr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM