0

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

Majed Badawi
  • 27,616
  • 4
  • 25
  • 48

1 Answers1

0

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>
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48