How do you sort an array alphabetically using sort_by in ruby? How do you sort an array alphabetically using sort_by in ruby? ruby ruby

How do you sort an array alphabetically using sort_by in ruby?


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 }


Is the sort method an option?

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


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] }