1

I have an array of arrays of the following kind

let finalData = [[1920000000, 273.15], [1620033000, 276.15], [1627700000, 272.15]]

The first value inside the data array is the timestamp. How do I sort the entire array from smallest to largest timestamp, with or without lodash?

Expected result:

let finalData = [[1620033000, 276.15], [1627700000, 272.15], [1920000000, 273.15]]

Thank you in advance for your help

1
  • 2
    Have you tried finalData.sort()? Commented May 3, 2021 at 9:54

1 Answer 1

1

Using Array#sort:

const finalData = [[1920000000, 273.15], [1620033000, 276.15], [1627700000, 272.15]];
finalData.sort(([a], [b]) => a - b);

console.log(finalData);

Using lodash:

let finalData = [[1920000000, 273.15], [1620033000, 276.15], [1627700000, 272.15]];
finalData = _.sortBy(finalData, e => e[0]);

console.log(finalData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous"></script>

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.