按created_at日期的降序排列对象数组

General_9

我有一个从Mongoid检索到的Comment对象数组。如何按created_at日期按降序对数组进行排序。

我试过下面的代码:

all_comments = []
    all_comments.concat(question_comments).concat(answer_comments).sort_by { |x| -x[:created_at] }

我收到以下错误:

2013-08-17 10:34:46 UTC:Time的未定义方法`-@'

桑托什

您可以使用desc方法。

all_comments.concat(question_comments).concat(answer_comments).desc(:created_at)

如果结果集为Array,则可以使用sort

all_comments.concat(question_comments).concat(answer_comments).sort { |x,y| y.created_at <=> x.created_at }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章