autos = [{:id => 1, :name => 'toyota'}, {:id => 2, :name => 'ford' }, {:id
=> 3, :name => 'toyota'} ]
autos.group_by {|h| h[:name]}.collect {|k,v| v} #=> [[{:name=>"toyota",
:id=>1}, {:name=>"toyota", :id=>3}], [{:name=>"ford", :id=>2}]]
On Wed, Mar 26, 2008 at 12:12 PM, tommy <[EMAIL PROTECTED]> wrote:
>
> hey guys,
> got an array question:
>
> class Autos
> with a variable name
>
> I have an array of Autos. I want to split this array into chunks of
> arrays grouped by name.
>
> ie, Autos = [{id:1, name:toyota}, {id:2, name:ford}, {id:3,
> name:toyota} ]
> would create two arrays in a super array
> [ [{id:1, name:toyota},{id:3, name:toyota}] , [{id:2, name:ford:}] ]
>
> i have the unique list of names.
>
> I was roughly thinking of sorting the array by name and finding the
> first occurrence of each name and doing a slice.
>
> Is there a faster approach?
> >
>
--
Jordan A. Fowler
2621 First Ave Apt 5
San Diego, CA 92103
E-mail: [EMAIL PROTECTED]
Website: http://www.jordanfowler.com
Phone: 406.546.8055
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---