如何根据对象的属性对对象数组进行排序?

时间:2022-02-26 13:18:15

I'm performing this query in a controler and the 'Group' model 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)?

用户数组如何被“lastname”排序?我试过几种不同的方法,但运气不好。试图根据数组中的对象属性进行排序,这让我很困惑。另外,我也不知道如何使用一个:查询中的顺序(如何:命令不是组而是每个组的用户)?

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 !):

也许我没有正确地引用对象的名称(“User”)?这似乎是可行的,因为它会产生一个“无方法”错误(或者如果使用‘sort_by’而没有!)

 users.sort_by! {|User| User.lastname}

Thanks for any help.

感谢任何帮助。

4 个解决方案

#1


30  

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 }

#2


28  

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!“#”errror:

users.sort_by! &:lastname

#3


15  

It's probably because you've got |User| when User is a class. Try changing it to an un-used variable name, like u, to get:

这可能是因为当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块一样,比如||端,或者任何其他{||}样式的块。

#4


2  

You can try putting an order clause in the "has_many :users" line that you have in your Group model

您可以尝试在您的组模型中拥有的“has_many:users”行中放置一个order子句

#1


30  

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 }

#2


28  

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!“#”errror:

users.sort_by! &:lastname

#3


15  

It's probably because you've got |User| when User is a class. Try changing it to an un-used variable name, like u, to get:

这可能是因为当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块一样,比如||端,或者任何其他{||}样式的块。

#4


2  

You can try putting an order clause in the "has_many :users" line that you have in your Group model

您可以尝试在您的组模型中拥有的“has_many:users”行中放置一个order子句