# Hash and Array sorting - a bit confused

**URL:** <https://www.ruby-forum.com/t/hash-and-array-sorting-a-bit-confused/174836>\
**Category:** Ruby\
**Created:** [September 2, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/hash-and-array-sorting-a-bit-confused/174836 "2009-09-02T21:41:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 2, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/hash-and-array-sorting-a-bit-confused/174836/1 "2009-09-02T21:41:49Z")

</div>

So, I have the following model method:

def self.cupcake\_report\_list  
list = Schedule.find\_all\_by\_division(2, :select =\>  
‘schedules.team\_id, schedules.cupcake’, :order =\> :team\_id)  
cupcakes = []  
list.each do |row|  
teamname = ConferenceRecord.find\_by\_team\_id(row.team\_id, :joins  
=\> [:team, :conference])  
cupcakes \<\< {‘name’=\> teamname.team.name,  
‘teamid’=\> teamname.team\_id,  
‘cupcake’ =\> row.cupcake,  
‘conference’ =\> teamname.conference.short\_name,  
‘confid’ =\> teamname.conference\_id}  
end  
return cupcakes  
end

This returns and array of hashes:

120 rows: 0…119  
Each row containing 5 hash key/values

Example:

test = cupcake\_report\_list

=\> {“name”=\>“Georgia Tech”, “cupcake”=\>“Jacksonville St.”,  
“conference”=\>“ACC”, “teamid”=\>4, “confid”=\>2}

What I want to do is sort the entire array by the name hash key so that  
if I were to look within the array, it would like so:

test[0]  
=\> {“name”=\>“Air Force”, etc., etc., etc., etc.}  
test[1]  
=\> {“name”=\>“Akron”, etc., etc., etc., etc.}  
test[2]  
=\> {“name”=\>“Alabama”, etc., etc., etc., etc.}

etc…

I can’t figure out a way to do that correctly by a specific hash key  
within the array.

Any ideas on how I can accomplish this?

Many thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 2, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/hash-and-array-sorting-a-bit-confused/174836/2 "2009-09-02T22:32:17Z")

</div>

Alpha B. wrote:

> Example:
> 
> test = cupcake\_report\_list
> 
> =\> {“name”=\>“Georgia Tech”, “cupcake”=\>“Jacksonville St.”,  
> “conference”=\>“ACC”, “teamid”=\>4, “confid”=\>2}
> 
> What I want to do is sort the entire array by the name hash key so that  
> if I were to look within the array, it would like so:
> 
> test[0]  
> =\> {“name”=\>“Air Force”, etc., etc., etc., etc.}  
> test[1]  
> =\> {“name”=\>“Akron”, etc., etc., etc., etc.}  
> test[2]  
> =\> {“name”=\>“Alabama”, etc., etc., etc., etc.}
> 
> etc…
> 
> I can’t figure out a way to do that correctly by a specific hash key  
> within the array.
> 
> Any ideas on how I can accomplish this?
> 
> Many thanks.

test = [  
{“name”=\>“Zakron”, “a” =\> “apple”},  
{“name”=\>“Akron”, “a” =\> “goodbye”},  
{“name”=\>“Air Force”, “a” =\> “hello”}  
]

result = test.sort\_by do |hash|  
hash[“name”]  
end

p result

–output:–  
[{“name”=\>“Air Force”, “a”=\>“hello”}, {“name”=\>“Akron”, “a”=\>“goodbye”},  
{“name”=\>“Zakron”, “a”=\>“apple”}]

result = test.sort\_by do |hash|  
hash[“name”]  
end

p result

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 2, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/hash-and-array-sorting-a-bit-confused/174836/3 "2009-09-02T23:27:26Z")

</div>

Wow, why does it have to be so simple. 🙂

Thanks a ton. If you knew what I was trying to do to get this to work,  
well, we’ll pretend I didn’t try all that stuff. 🙂

Thanks mate.
