0

https://ramdajs.com/docs/#sort

How can I use this to sort this array?

const prices = [
      {
        date: "2020-07-27",
        value: 157,
      },
      {
        date: "2020-07-26",
        value: 157,
      },
      {
        date: "2020-07-28",
        value: 157,
      },
    ];

const expectedOutput = [
      {
        date: "2020-07-28",
        value: 157,
      },
      {
        date: "2020-07-27",
        value: 157,
      },
      {
        date: "2020-07-26",
        value: 157,
      },
    ];
Joe
  • 4,274
  • 32
  • 95
  • 175
  • 1
    Does this answer your question? [How to sort array by date](https://stackoverflow.com/questions/44582097/how-to-sort-array-by-date) – Heretic Monkey Jul 28 '20 at 15:05
  • Note that the function passed to Ramda's `sort` function is the same as that passed to the native `Array.prototype.sort` function. – Heretic Monkey Jul 28 '20 at 15:06
  • Mm. But I don't get it how to implement it with ramda – Joe Jul 28 '20 at 15:08
  • 1
    Another Ramda possibility: `sort (descend (prop ('date'))) (prices)`. For an ascending sort, it's easier, `sort (prop ('date')) (prices)`. – Scott Sauyet Jul 28 '20 at 15:52

1 Answers1

2

Based on what I understand, I think you can do that with Date API like

const diff = function(a, b){
 return new Date(a.date).getTime() - new Date(b.date).getTime()
}

R.sort(diff,prices);

If this answer is fall short of your expectation, please comment to me

Stark Jeon
  • 1,107
  • 9
  • 24