0

I have an array for a timeline chart of Google Charts

So the array looks like follows

let data = [
    ['Outreach ICU Follow-up', '2021-01-25', 2021-02-01],
    ['Hospital Stay'         , '2021-01-01', '2021-03-01'],
    ['ICU Stay'              , '2021-01-02', '2021-01-25'],
    ['Outreach ICU Follow-up', '2021-02-20', '2021-03-01'],
    ['ICU Stay'              , '2021-02-01', '2021-02-20'],
]

I lined up the date to see the chronological order. I need to sort this by the first data ( element 1 )

Cant use this because its for objects I think.

let ass = data.sort((x,y)=>y.dlk_assess_order-x.dlk_assess_order);

And this one is futile as-well.

var data = data.sort(function(a, b) { return a - b; });

How does one sort and array like this please? Is there perhaps a lodash function?

morne
  • 4,035
  • 9
  • 50
  • 96
  • 1
    what is `dlk_assess_order`? why not sort by exactly what you said `x[1]` and `y[1]`? – Jamiec Jun 15 '21 at 15:24
  • Perhaps I should have thought of that hey. Yep that worked. – morne Jun 15 '21 at 15:26
  • Go ahead with the answer, I'll mark you up. – morne Jun 15 '21 at 15:26
  • Rather than hoping for a perfect example to copy and paste, spend some time understanding what the code you've already found does, and you'll realise that what you want is a trivial modification. There are also [lots of examples on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). – IMSoP Jun 15 '21 at 15:27
  • https://jsfiddle.net/factedu/5ekcwto1/3/ – Ravi S. Singh Jun 15 '21 at 15:48

1 Answers1

1

As your question said, you simply sort by the element at position 1

let data = [
    ['Outreach ICU Follow-up', '2021-01-25', '2021-02-01'],
    ['Hospital Stay'         , '2021-01-01', '2021-03-01'],
    ['ICU Stay'              , '2021-01-02', '2021-01-25'],
    ['Outreach ICU Follow-up', '2021-02-20', '2021-03-01'],
    ['ICU Stay'              , '2021-02-01', '2021-02-20'],
]

const sorted = data.sort((a,b) => a[1] - b[1]);
console.log(sorted);
Det
  • 3,640
  • 5
  • 20
  • 27
Jamiec
  • 133,658
  • 13
  • 134
  • 193