29

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
  • Group has_many :memberships

@memberships is equal to:

[
  {
  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 }

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

Any help would be much appreciated.

Brian Weinreich
  • 7,692
  • 8
  • 35
  • 59

3 Answers3

71

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.

@memberships.sort_by!{ |m| m.group.name.downcase }
Brian Weinreich
  • 7,692
  • 8
  • 35
  • 59
3

Is the sort method an option?

ary.sort{ |a,b| a[:group][:name] <=> b[:group][:name] }
Angelo
  • 492
  • 3
  • 5
  • I tried that as well.. `@memberships.sort { |a,b| a.group.name <=> b.group.name }` still doesn't work tho. – Brian Weinreich Jul 17 '12 at 21:28
  • Forgot the nesting... I corrected by adding the parent group key – Angelo Jul 17 '12 at 21:31
  • Okay, maybe this is the problem– something I didn't mention. When I do a.group.name I am performing a join between the memberships and groups table. So, would that have an effect on the result sorting? Cause it still won't work with the updated code. – Brian Weinreich Jul 17 '12 at 21:33
  • So you're not sorting the resulting array of hashes, but the ActiveRecord objects, correct? Maybe try using "order" `Membership.joins(:groups).order("groups.name")` – Angelo Jul 17 '12 at 21:52
  • I ended up figuring it out. It was a stupid problem. I didn't realize sort didn't take into account upper and lower case. So I could figure out why it looked like this: [Ash, Bob, Jim, anna, kim] – I ended up sorting by downcase. Thanks for your help (sorry to bother ya with something silly like this!) – Brian Weinreich Jul 18 '12 at 14:39
1

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

Here's a working syntax

@memberships.sort_by!{ |m| m[:group][:name] }
Dty
  • 12,253
  • 6
  • 43
  • 61