简体   繁体   English

我想使用 ramda.js 按升序对日期进行排序

[英]I want to sort the dates in ascending order using ramda.js

I tried the following code but it doesn't work:我尝试了以下代码,但它不起作用:

const startDate = ["2020-07-17", "2090-09-27", "1920-12-11"]

const pathComponents = R.split('-');
  const sortDate = R.sortBy(R.ascend(pathComponents(R.prop(startDate))));


const sortDate = R.sortBy(R.prop('startDate'));

const sortDate = R.sortBy(R.descend( R.prop('startDate')));

You can use sort :您可以使用排序

R.sort((a,b)=>new Date(b) - new Date(a), ["10 june 1859", "12 august 1387", "30 december 1998"]); 

If you want it in descent just change new Date(b) - new Date(a) for new Date(a) - new Date(b)如果您希望它下降,只需将new Date(b) - new Date(a)更改为new Date(a) - new Date(b)

Snippet of the result with javascript vanilla: javascript vanilla 的结果片段:

 console.log(["10 june 1859", "12 august 1387", "30 december 1998"].sort((a,b)=>new Date(b) - new Date(a)))

I'm missing something here.我在这里遗漏了一些东西。 I don't know if this is all you want to do, or if there is something additional going on:我不知道这是否是您想要做的,或者是否还有其他事情发生:

 const startDate = ["2020-07-17", "2090-09-27", "1920-12-11"] const sortDate = sort (descend (identity), startDate) console.log (sortDate)
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script> <script> const {sort, descend, identity} = R </script>

Dates in that format are already intrinsically sortable;这种格式的日期本质上是可排序的; that's part of the point of the format.这是格式要点的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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