如何使用ruby中的sort_by按字母顺序对数组进行排序?

时间:2022-08-20 15:57:54

I have an array of memberships. In each membership is a group. I need to sort this array of memberships by the name of the group. I've tried a bunch of different ways, and the latest way is this:

我有一系列的会员资格。每个成员都是一个团体。我需要按照组的名称对这个成员数组进行排序。我尝试了很多不同的方法,最新的方法是:

@memberships.sort_by! { |m| m.group.name }

However, this doesn't sort by the name. It appears to be randomly sorting the array.

但是,这并不是按名称排序的。它似乎是随机排序数组。

  • Membership belongs_to :group
  • 成员资格belongs_to:group
  • Group has_many :memberships
  • 集团has_many:会员资格

@memberships is equal to:

@memberships等于:

[
  {
  id: 2141,
  user_id: 491,
  group_id: 271,
  member_type: "member",
    group: {
      id: 271,
      name: "Derek's",
      privacy: "open",
      bio_image_url: "/bio_images/medium/missing.png?1340285189",
      member_count: 1,
      upcoming_checkins_count: 0
    }
  },
  {
  id: 2201,
  user_id: 221,
  group_id: 291,
  member_type: "member",
    group: {
      id: 291,
      name: "Rounded Developement",
      privacy: "closed",
      bio_image_url: "/groups/medium/291/bioimage.jpg?1340736175",
      member_count: 7,
      upcoming_checkins_count: 0
   }
}
]

NOTE: This does work --> @memberships.sort_by! { |m| m.group.id }

注意:这确实有效 - > @ memberships.sort_by! {| m | m.group.id}

It will order the array based on the group.id so maybe it has something to do with sorting alphabetically?

它将基于group.id对数组进行排序,所以它可能与按字母顺序排序有关吗?

Any help would be much appreciated.

任何帮助将非常感激。

3 个解决方案

#1


66  

Wow, after struggling with this for an extremely long time, I realized my problem was a simple one. I was sorting by group.name but some of the group names were uppercase and some were lower, which was throwing it all off. Converting everything to downcase worked well.

哇,经过这么长时间的努力,我意识到我的问题很简单。我按group.name排序,但是一些组名是大写的,有些是较低的,这就是把它全部抛弃了。将所有内容转换为downcase运行良好。

@memberships.sort_by!{ |m| m.group.name.downcase }

#2


3  

Is the sort method an option?

排序方法是一种选择吗?

ary.sort{ |a,b| a[:group][:name] <=> b[:group][:name] }

#3


1  

I don't see how your code is working. I can't access the hashes in the arrays using m.group.name

我看不出你的代码是如何工作的。我无法使用m.group.name访问数组中的哈希值

Here's a working syntax

这是一个有效的语法

@memberships.sort_by!{ |m| m[:group][:name] }

#1


66  

Wow, after struggling with this for an extremely long time, I realized my problem was a simple one. I was sorting by group.name but some of the group names were uppercase and some were lower, which was throwing it all off. Converting everything to downcase worked well.

哇,经过这么长时间的努力,我意识到我的问题很简单。我按group.name排序,但是一些组名是大写的,有些是较低的,这就是把它全部抛弃了。将所有内容转换为downcase运行良好。

@memberships.sort_by!{ |m| m.group.name.downcase }

#2


3  

Is the sort method an option?

排序方法是一种选择吗?

ary.sort{ |a,b| a[:group][:name] <=> b[:group][:name] }

#3


1  

I don't see how your code is working. I can't access the hashes in the arrays using m.group.name

我看不出你的代码是如何工作的。我无法使用m.group.name访问数组中的哈希值

Here's a working syntax

这是一个有效的语法

@memberships.sort_by!{ |m| m[:group][:name] }