# Quick way to split up an array of objects into groups?

**URL:** <https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689>\
**Category:** Ruby\
**Created:** [January 21, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689 "2008-01-21T12:23:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/1 "2008-01-21T12:23:36Z")

</div>

I have a collection of objects, which i want to separate into groups  
based on a particular property (to be specific, two attributes - that is  
if obj.foo and obj.bar are both the same for two objects they go into an  
array together). Not every object has a duplicate, but most of them do.  
So, i’d end up with an array, where each member is another array, with  
the subarrays holding either 1 or 2 (or maybe more) objects that match  
on foo and bar.

Can anyone tell me a nice and simple way of doing this?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/2 "2008-01-21T12:29:47Z")

</div>

On Jan 21, 2008, at 12:23 PM, Max W. wrote:

> Can anyone tell me a nice and simple way of doing this?

Enumerable#parition or Set#classify might help.

– fxn

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/3 "2008-01-21T14:08:03Z")

</div>

Xavier N. wrote:

> On Jan 21, 2008, at 12:23 PM, Max W. wrote:
> 
> > Can anyone tell me a nice and simple way of doing this?
> 
> Enumerable#parition or Set#classify might help.
> 
> – fxn

hmmm…i couldn’t work out how to make it easier with either of those.  
I ended up doing this, which isn’t very clever but it did seem to work.

groups = []  
vals = collection.collect{|obj| [obj.val1, obj.val2]}.uniq!  
for val in vals  
groups \<\< collection.select{|obj| obj.val1 == val[0] && obj.val2 =  
val[1]  
end

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/4 "2008-01-21T14:27:50Z")

</div>

Rick Denatale wrote:

> The activesupport gem, which is part of Rails, but can be used  
> separately, adds an Enumerable#group\_by method which does pretty much  
> what you are asking for if I understand correctly.
> 
> –  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

Ah right, i see now 🙂 cheers!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/5 "2008-01-21T14:18:06Z")

</div>

On Jan 21, 2008 8:07 AM, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hmmm…i couldn’t work out how to make it easier with either of those.  
> I ended up doing this, which isn’t very clever but it did seem to work.
> 
> groups = []  
> vals = collection.collect{|obj| [obj.val1, obj.val2]}.uniq!  
> for val in vals  
> groups \<\< collection.select{|obj| obj.val1 == val[0] && obj.val2 =  
> val[1]  
> end

The activesupport gem, which is part of Rails, but can be used  
separately, adds an Enumerable#group\_by method which does pretty much  
what you are asking for if I understand correctly.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/6 "2008-01-21T15:54:28Z")

</div>

Xavier N. wrote:

> Note there is an assignment instead of an equals operator there.
> 
> > end
> 
> Yes, I think that’s equivalent to
> 
> collection.to\_set.classify {|x| [x.foo, x.bar]}.values
> 
> plus perhaps a map to\_a.
> 
> – fxn

That’s not really my code, it’s a paraphrased version for clarity 🙂  
Cheers for the tip about to\_set.classify though.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 21, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/quick-way-to-split-up-an-array-of-objects-into-groups/126689/7 "2008-01-21T15:40:32Z")

</div>

On Jan 21, 2008, at 2:07 PM, Max W. wrote:

> hmmm…i couldn’t work out how to make it easier with either of those.  
> I ended up doing this, which isn’t very clever but it did seem to  
> work.
> 
> groups = []  
> vals = collection.collect{|obj| [obj.val1, obj.val2]}.uniq!  
> for val in vals  
> groups \<\< collection.select{|obj| obj.val1 == val[0] && obj.val2 =  
> val[1]

Note there is an assignment instead of an equals operator there.

> end

Yes, I think that’s equivalent to

collection.to\_set.classify {|x| [x.foo, x.bar]}.values

plus perhaps a map to\_a.

– fxn
