From: Robert Dober Date: 2008-08-28T21:10:51+09:00 Subject: Re: Sort an array based on an attribute of its objects On Thu, Aug 28, 2008 at 1:58 PM, David A. Black wrote: David beat me to the line, but as somebody will post an inject based solution anyway, why not let it be me ;). class Array def group_by message = nil inject( Hash.new {|h,k| h[k] = [] } ){ |h, e| h[message ? e.send( message ) : yield(e)] << e h } end end p %w{ Hello Brave GNU World }.group_by {|o| o.size } p %w{ Hello Brave GNU World }.group_by( :size ) Cheers Robert -- http://ruby-smalltalk.blogspot.com/ There's no one thing that's true. It's all true. -- Ernest Hemingway