Joe
Joe

Reputation: 4234

Sort by date, descending in ramda js

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,
      },
    ];

Upvotes: 0

Views: 2291

Answers (1)

Stark Jeon
Stark Jeon

Reputation: 1145

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

Upvotes: 2

Related Questions