Rails, Ruby,如何对数组排序?

时间:2022-01-31 14:39:33

in my rails app I'm creating an array like so:

在我的rails应用中,我创建了一个这样的数组:

@messages.each do |message|

  @list << {
    :id => message.id,
    :title => message.title,
    :time_ago => message.replies.first.created_at
  }
end

After making this array I would like to then sort it by time_ago ASC order, is that possible?

在创建这个数组之后,我想按time_ago ASC顺序对它进行排序,这可能吗?

6 个解决方案

#1


122  

 @list.sort_by{|e| e[:time_ago]}

it defaults to ASC, however if you wanted DESC you can do:

它默认为ASC,但是如果你想要DESC,你可以:

 @list.sort_by{|e| -e[:time_ago]}

Also it seems like you are trying to build the list from @messages. You can simply do:

同样,您似乎正在尝试从@messages构建列表。你可以做的:

@list = @messages.map{|m| 
  {:id => m.id, :title => m.title, :time_ago => m.replies.first.created_at }
}

#2


10  

You could do:

你能做的:

@list.sort {|a, b| a[:time_ago] <=> b[:time_ago]}

#3


5  

You can also do @list.sort_by { |message| message.time_ago }

你也可以做@list。sort_by { |消息|消息。time_ago }

#4


4  

In rails 4+

在rails中4 +

@list.sort_by(&:time_ago)

#5


3  

Just FYI, I don't see the point in moving the messages into a new list and then sorting them. As long as it is ActiveRecord it should be done directly when querying the database in my opinion.

仅供参考,我不认为将消息移动到新的列表中然后对它们进行排序有什么意义。在我看来,只要是ActiveRecord,在查询数据库时就应该直接执行。

It looks like you should be able to do it like this:

看起来你应该可以这样做:

@messages = Message.includes(:replies).order("replies.created_at ASC")

That should be enough unless I have misunderstood the purpose.

那就足够了,除非我误解了目的。

#6


1  

Yes, you can use group_by :

是的,你可以使用group_by:

http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by

http://api.rubyonrails.org/classes/Enumerable.html method-i-group_by

#1


122  

 @list.sort_by{|e| e[:time_ago]}

it defaults to ASC, however if you wanted DESC you can do:

它默认为ASC,但是如果你想要DESC,你可以:

 @list.sort_by{|e| -e[:time_ago]}

Also it seems like you are trying to build the list from @messages. You can simply do:

同样,您似乎正在尝试从@messages构建列表。你可以做的:

@list = @messages.map{|m| 
  {:id => m.id, :title => m.title, :time_ago => m.replies.first.created_at }
}

#2


10  

You could do:

你能做的:

@list.sort {|a, b| a[:time_ago] <=> b[:time_ago]}

#3


5  

You can also do @list.sort_by { |message| message.time_ago }

你也可以做@list。sort_by { |消息|消息。time_ago }

#4


4  

In rails 4+

在rails中4 +

@list.sort_by(&:time_ago)

#5


3  

Just FYI, I don't see the point in moving the messages into a new list and then sorting them. As long as it is ActiveRecord it should be done directly when querying the database in my opinion.

仅供参考,我不认为将消息移动到新的列表中然后对它们进行排序有什么意义。在我看来,只要是ActiveRecord,在查询数据库时就应该直接执行。

It looks like you should be able to do it like this:

看起来你应该可以这样做:

@messages = Message.includes(:replies).order("replies.created_at ASC")

That should be enough unless I have misunderstood the purpose.

那就足够了,除非我误解了目的。

#6


1  

Yes, you can use group_by :

是的,你可以使用group_by:

http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by

http://api.rubyonrails.org/classes/Enumerable.html method-i-group_by