【发布时间】:2023-03-16 07:22:02
【问题描述】:
我有一个约会集合,其中有一个start_date DateTime 字段。
我需要对这些约会进行排序,所以我有
- 即将到来的约会优先
- 然后是“过去的”约会,最近的约会在前
假设我有
我使用相对时间来举例说明,请记住我必须与 Time.now 进行比较的代码
[ in_two_days, two_days_ago, tomorrow, yesterday]
我希望排序返回
[
# First upcoming
tomorrow,
in_two_days,
# Then most recent first
yesterday,
two_days_ago
]
我正在使用 Mongoid,但由于数组中的项目很少,我会接受使用数组方法而不是标准的解决方案(尽管使用 mongoid 标准的解决方案会更好)
【问题讨论】:
标签: ruby-on-rails sorting mongoid ruby-on-rails-5 mongoid5