Ruby-on-rails 如何按对象的属性对对象数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4610843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort an array of objects by an attribute of the objects?
提问by Reno
I'm performing this query in a controler and the 'Group' model has_many Users
我在控制器中执行此查询,“组”模型 has_many Users
@group= Group.find(params[:id])
@group is being used to render this partial (the partial dumps the users of a group into a table)
@group 用于呈现此部分(部分将组的用户转储到表中)
<%= render :partial=>"user_list", :locals=>{:users=>@group.users} %>
The local variable 'users' passed to the partial is an array of User objects;
传递给部分的局部变量“用户”是一个用户对象数组;
- !ruby/object:User
attributes:
updated_at: 2011-01-04 21:12:04
firstname: Bob
lastname: Smith
id: "15"
group_id: "2"
created_at: 2010-11-26 12:54:45
How can the user array be sorted by 'lastname'? I've tried several different ways without any luck. Trying to sort by a object attribute inside an array in confusing me. Also, I don't undertand how I could do this with an :order in the query (how to :order not the Group but the Users of each group)?
用户数组如何按“姓氏”排序?我尝试了几种不同的方法,但没有任何运气。试图按数组内的对象属性进行排序让我感到困惑。另外,我不明白如何在查询中使用 :order 来执行此操作(如何 :order 不是组而是每个组的用户)?
Maybe I'm not referring to name of the object correctly ('User')? It seems like this should work be it produces a 'no method' error (or a dynamic constant assignment error if 'sort_by' is used without the !):
也许我没有正确引用对象的名称(“用户”)?看起来这应该有效,因为它会产生“无方法”错误(或者如果使用“sort_by”而没有 !),则会产生动态常量分配错误:
users.sort_by! {|User| User.lastname}
Thanks for any help.
谢谢你的帮助。
回答by Reno
I found a method that works from here. I don't understand what the "&" symbol is doing - maybe it's shorthad for "object" since in my case ":lastname" is an attribute of the object that make up the array.
我找到了一种从这里开始工作的方法。我不明白“&”符号在做什么 - 也许它是“object”的缩写,因为在我的例子中“:lastname”是组成数组的对象的一个属性。
users = users.sort_by &:lastname
note: I don't undertand why but a destructive version won't work. It produces a "undefined method `sort_by!' for #" errror:
注意:我不明白为什么,但破坏性版本不起作用。它产生一个“未定义的方法`sort_by!' 对于 #" 错误:
users.sort_by! &:lastname
回答by BvuRVKyUVlViVIc7
See http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
见http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
@users.sort! { |a,b| a.name.downcase <=> b.name.downcase }
回答by Groxx
It's probably because you've got |User|when Useris a class. Try changing it to an un-used variable name, like u, to get:
这可能是因为你有|User|什么时候User上课。尝试将其更改为未使用的变量名称,例如u,以获得:
users.sort_by! {|u| u.lastname}
The term between the pipes is the name of the variable each item is stored in, as it runs through, not the type of object you've got. This is the same as other Ruby blocks, like do || end, or any other {||}-style block.
管道之间的术语是每个项目在其中运行时存储的变量的名称,而不是您拥有的对象类型。这与其他 Ruby 块相同,例如do || end,或任何其他{||}样式的块。
回答by Aditya Sanghi
You can try putting an order clause in the "has_many :users" line that you have in your Group model
您可以尝试在 Group 模型中的“has_many :users”行中放置一个 order 子句

