0
我有两个Mongoid文件数组,FirstType
文件和SecondType
文件。两者都嵌入在User
中。如何对mongoid文档数组进行排序(数组包含不同的mongoid文档类型)?
user.first_types = [#<FirstType _id: 51a10b4883c336ebef0002a8, created_at: 2013-05-25 19:04:40 UTC, updated_at: 2013-05-25 19:04:40 UTC, datetime: 2013-03-28 15:03:22 UTC, text: "Hello 1">]
user.second_types = [#<SecondType _id: 51a6058783c3368a62000003, created_at: 2013-05-29 13:41:27 UTC, updated_at: 2013-05-29 13:41:27 UTC, datetime: 2013-05-29 08:23:27 UTC, text: "Hello 2">]
然后我将它们合并与此行代码:
all_types = user.first_types+user.second_types
我现在要由属性datetime
(最新datetime
第一all_types数组进行排序,所以SecondType
对象应该是第一个)。
我已经尝试了传统的Mongoid和Ruby数组方法,但是当我混淆Mongoid文档时他们似乎不工作。有任何想法吗?
为什么'-x.datetime.to_f'不仅'x.datetime'? ? –
@Ruby爱:降序“最新日期时间第一”。 – maerics
为什么不'all_types.sort_by {| x | x.datetime} .reverse'? –