# Hash custom sort? Overriding \<=\>? Help!

**URL:** <https://www.ruby-forum.com/t/hash-custom-sort-overriding-help/148012>\
**Category:** Ruby\
**Created:** [September 8, 2008, 9:46am UTC](https://www.ruby-forum.com/t/hash-custom-sort-overriding-help/148012 "2008-09-08T09:46:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blink](https://www.ruby-forum.com/u/blink)\
**Post date:** [September 8, 2008, 9:46am UTC](https://www.ruby-forum.com/t/hash-custom-sort-overriding-help/148012/1 "2008-09-08T09:46:00Z")

</div>

This is a pretty ugly hash. The keys are objects. I’m afraid after going  
down a few paths, it was a bit over my head. Here’s the hash:

[[#\<Company id: 13, name: “Company Test 1”, created\_at: “2008-09-03  
04:05:58”, updated\_at: “2008-09-03 04:05:58”, account\_id: 16,  
account\_company: false\>, [#\<Project id: 34, name: “Test”, created\_at:  
“2008-09-07 04:54:55”, updated\_at: “2008-09-07 04:54:55”, account\_id:  
16, primary\_company\_id: 13\>]], [#\<Company id: 12, name: “Company Test  
2”, created\_at: "…

Yeah. I know…

So briefly, the keys look like is #\<Company id: 13, name: “Company Test  
1”, … \>.

I need to sort by ‘name’. Though another question remains that would  
make this all moot. I use:

@project\_companies = @projects.group\_by(&:primary\_company)

where :primary\_company is a relationship situation. I found I can use  
either the foreign key, (:primary\_company\_id, which would just give me  
the id), or the above which gives me the whole enitre object as a key.  
But is there any in between? Something like:

@project\_companies = @projects.group\_by(&:primary\_company.name)

If not, how can I create my own sort using \<=\>??? I found a bit in the  
rdoc, and well now I’m here.

---

<div class="post-metadata">

**Author:** ![blink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blink](https://www.ruby-forum.com/u/blink)\
**Post date:** [September 8, 2008, 10:09am UTC](https://www.ruby-forum.com/t/hash-custom-sort-overriding-help/148012/2 "2008-09-08T10:09:01Z")

</div>

Hi –

On Mon, 8 Sep 2008, Jarrett G. wrote:

> Yeah. I know…  
> either the foreign key, (:primary\_company\_id, which would just give me  
> the id), or the above which gives me the whole enitre object as a key.  
> But is there any in between? Something like:
> 
> @project\_companies = @projects.group\_by(&:primary\_company.name)
> 
> If not, how can I create my own sort using \<=\>??? I found a bit in the  
> rdoc, and well now I’m here.

Sorting and grouping aren’t the same thing, but if you want grouping  
by the results of the name method, you would do:

@project\_companies = @projects.group\_by {|project| project.name }

David

---

<div class="post-metadata">

**Author:** ![blink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blink](https://www.ruby-forum.com/u/blink)\
**Post date:** [September 8, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/hash-custom-sort-overriding-help/148012/3 "2008-09-08T22:21:03Z")

</div>

David A. Black wrote:

> Sorting and grouping aren’t the same thing, but if you want grouping  
> by the results of the name method, you would do:
> 
> @project\_companies = @projects.group\_by {|project| project.name }

Thanks for the response david! Unfortunately, I don’t think this is what  
I was looking for. The key I need exists through an association.

Calling project.name gives me the project’s name, sure, and calling  
project.primary\_company\_id, gives me the foreign key id of a has\_one  
relationship with Company. calling project.primary\_company, through some  
rails magic gives me the entire object associated with that foreign key.  
I’d like that key to just be one parameter out of that relationship -  
the associated company’s name, which was why I was asking if I could use  
@project\_companies = @projects.group\_by(&:primary\_company.name). It  
seems to be an all or nothing. I can either have the foreign key  
integer, or the entire object, but not param from that object?

---

<div class="post-metadata">

**Author:** ![blink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@blink](https://www.ruby-forum.com/u/blink)\
**Post date:** [September 9, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/hash-custom-sort-overriding-help/148012/4 "2008-09-09T15:12:28Z")

</div>

2008/9/8 Jarrett G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks for the response david! Unfortunately, I don’t think this is what  
> integer, or the entire object, but not param from that object?  
> Why can’t you do

@project\_companies = @projects.group\_by {|pr| pr.primary\_company.name}

?

robert
