第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

按日期對(duì) desc 進(jìn)行排序,如果并列則按 javascript 數(shù)組中的風(fēng)險(xiǎn)排序

按日期對(duì) desc 進(jìn)行排序,如果并列則按 javascript 數(shù)組中的風(fēng)險(xiǎn)排序

我有一組對(duì)象,我想首先按日期排序,即 desc 中的“create_date_format”,如果領(lǐng)帶則按風(fēng)險(xiǎn)字母順序排序,即 asc我嘗試了 lodash.orderBy(risk_list, ['create_date_format', 'risk'], ['desc']) 但由于日期是字符串格式,它按日期字符串中的數(shù)字排序,因此例如 2020 年 1 月 28 日出現(xiàn)在 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"}]我想先按日期排序,即 desc 中的“create_date_format”,如果是,則在 asc 中按字母順序排列“風(fēng)險(xiǎn)”
查看完整描述

2 回答

?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊

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) 

)


查看完整回答
反對(duì) 回復(fù) 2023-04-01
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以做的是編寫一個(gè)排序函數(shù),該函數(shù)具有用于比較屬性的簡(jiǎn)單檢查。為了首先按日期排序,您需要將字符串日期轉(zhuǎn)換為實(shí)例Date,以便按時(shí)間而不是按字符對(duì)值進(jìn)行排序。


為要比較的兩個(gè)對(duì)象創(chuàng)建日期實(shí)例后,您可以檢查兩個(gè)日期項(xiàng)是否相等。如果是,則跳過此排序邏輯并執(zhí)行基于 的排序risk。查看代碼片段


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)


查看完整回答
反對(duì) 回復(fù) 2023-04-01
  • 2 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)