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?

5
  • 1
    what is dlk_assess_order? why not sort by exactly what you said x[1] and y[1]? Commented Jun 15, 2021 at 15:24
  • Perhaps I should have thought of that hey. Yep that worked. Commented Jun 15, 2021 at 15:26
  • Go ahead with the answer, I'll mark you up. Commented Jun 15, 2021 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. Commented Jun 15, 2021 at 15:27
  • jsfiddle.net/factedu/5ekcwto1/3 Commented Jun 15, 2021 at 15:48

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.