简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM