0

I've array of objects which i want to sort first on date i.e, 'create_date_format' in desc & if tie then sort on risk alphabetically i.e, 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 e.g 28 jan 2020 appears before 01 aug 2020 as 28 is higher than 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 i.e, 'create_date_format' in desc and if tie then on 'risk' alphabetically in asc

Scott Sauyet
  • 37,179
  • 4
  • 36
  • 82
  • you do not need an external library like lodash; all you need is `Array.prototype.sort()` (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) and, if you want to sort based on two condition, you impy do something like `arr.sort((a, b) => return [sorting condition 1] || [sorting condition 2])` – secan Sep 28 '20 at 13:17
  • This should help: https://stackoverflow.com/questions/10123953/how-to-sort-an-array-by-a-date-property – abalexandre Sep 28 '20 at 13:18

2 Answers2

1
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) 
)
ΔO 'delta zero'
  • 2,568
  • 1
  • 13
  • 22
0

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.

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. 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)
Matt Croak
  • 2,279
  • 2
  • 11
  • 22