# Hash Sorting

**URL:** <https://rubytalk.org/t/hash-sorting/42602>\
**Category:** ruby-talk\
**Created:** [29 November 2007 10:09 UTC](https://rubytalk.org/t/hash-sorting/42602 "2007-11-29T10:09:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Viswa](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Nathan\_Viswa](https://rubytalk.org/u/Nathan_Viswa)\
**Post date:** [29 November 2007 10:09 UTC](https://rubytalk.org/t/hash-sorting/42602/1 "2007-11-29T10:09:59Z")

</div>

Can not understand how the block after sort works! Need help. thanks.

&nbsp;&nbsp;&nbsp;h = { "a" =\> 20, "b" =\> 30, "c" =\> 10 }  
&nbsp;&nbsp;&nbsp;puts h.sort #=\> [["a", 20], ["b", 30], ["c", 10]]  
&nbsp;&nbsp;&nbsp;puts h.sort {|a,b| a[0]\<=\>b[0]} # as above  
&nbsp;&nbsp;&nbsp;puts h.sort {|a,b| a[1]\<=\>b[1]} #=\> [["c", 10], ["a", 20], ["b", 30]]

I:\RubyNV\>zzz-tut6-hash.rb  
a  
20  
b  
30  
c  
10

a  
20  
b  
30  
c  
10

c  
10  
a  
20  
b  
30

I:\RubyNV\>

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [29 November 2007 10:29 UTC](https://rubytalk.org/t/hash-sorting/42602/2 "2007-11-29T10:29:44Z")

</div>

Alle giovedì 29 novembre 2007, Nathan Viswa ha scritto:

> Can not understand how the block after sort works! Need help. thanks.
> 
> &nbsp;&nbsp;&nbsp;h = { "a" =\> 20, "b" =\> 30, "c" =\> 10 }  
> &nbsp;&nbsp;&nbsp;puts h.sort #=\> [["a", 20], ["b", 30], ["c", 10]]  
> &nbsp;&nbsp;&nbsp;puts h.sort {|a,b| a[0]\<=\>b[0]} # as above  
> &nbsp;&nbsp;&nbsp;puts h.sort {|a,b| a[1]\<=\>b[1]} #=\> [["c", 10], ["a", 20], ["b", 30]]

What Hash#sort does is:  
\* first: convert the hash into a nested array:  
&nbsp;&nbsp;[['a', 20], ['b', 30], ['c', 10]]  
\* second: sort that array according to the block.

In the first call to sort, you don't pass a block to it, so the sorting will  
be performed by calling the \<=\> of the contents of the array (i.e, on the  
key - value arrays). Array#\<=\> compares the contents of the two arrays in  
order, returning +1 or -1 as soon as one of the items is different from the  
other. For example, ['a', 20]\<=\>['c', 10] returns -1 because 'a'  
precedes 'c'. If two entries had the same first element (here this is  
impossible since they come from a hash), the second element would be  
compared, an so on.

Your second call to send gives the same result, because you're explicitly  
telling sort to compare only the first element of the array. As explained  
above, even in the blockless case sort never needs to check the second  
element, so the results are the same.

In the third case, you tell sort to compare the second element of each pair  
(i.e, the value in the original hash). This way, the pair ['c', 10] becomes  
the first, because 10 is the lesser of the three values; ['a', 20] is the  
second because 20 is the middle value and ['b', 30] is the last because 30 is  
the greater value.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [29 November 2007 12:05 UTC](https://rubytalk.org/t/hash-sorting/42602/3 "2007-11-29T12:05:24Z")

</div>

As can be read in the documentation:

Converts hsh to a nested array of [key, value] arrays and sorts it,  
using Array#sort.

This means that sort will convert the array to this (in some order):

[["a", 20], ["b", 30], ["c", 10]]

and then call array sort on it. Checking the documentation on Array#sort:

Returns a new array created by sorting self. Comparisons for the sort  
will be done using the \<=\> operator or using an optional code block.  
The block implements a comparison between a and b, returning -1, 0, or  
+1. See also Enumerable#sort\_by.

&nbsp;&nbsp;&nbsp;a = ["d", "a", "e", "c", "b"]  
&nbsp;&nbsp;&nbsp;a.sort #=\> ["a", "b", "c", "d", "e"]  
&nbsp;&nbsp;&nbsp;a.sort {|x,y| y \<=\> x } #=\> ["e", "d", "c", "b", "a"]

What this means is that in the block form, whenever the sort algorithm  
needs to compare two elements of the array, it will yield to the block  
passing both elements (remember that each element is itself an array  
of [key,value] generated by the hash) and expecting a -1, 0 or 1  
result, depending on which one you consider to be less than the other  
or if they are equal.

In the case of the hash example above:

h.sort {|a,b| a[0]\<=\>b[0]}

the block will receive, for example:  
a = ["a", 20]  
b = ["b", 30]

or

a = ["a", 20]  
b = ["c", 10]

or whatever pairs the sorting algorithm needs to compare. Then inside  
the block you need to provide the comparison of the two elements. The  
first example is doing a comparison of the keys, because the first  
element of the pair is the hash key, so:

a[0] \<=\> b[0]

calls the spaceship operator on the keys of the hash. While the second  
one a[1] \<=\> b[1] compares the values of the hash.

Hope this makes any sense.

Jesus.

> **···**
>
> On Nov 29, 2007 11:09 AM, Nathan Viswa \<nathanv@rogers.com\> wrote:
> 
> > Can not understand how the block after sort works! Need help. thanks.
> > 
> > &nbsp;&nbsp;&nbsp;h = { "a" =\> 20, "b" =\> 30, "c" =\> 10 }  
> > &nbsp;&nbsp;&nbsp;puts h.sort #=\> [["a", 20], ["b", 30], ["c", 10]]  
> > &nbsp;&nbsp;&nbsp;puts h.sort {|a,b| a[0]\<=\>b[0]} # as above  
> > &nbsp;&nbsp;&nbsp;puts h.sort {|a,b| a[1]\<=\>b[1]} #=\> [["c", 10], ["a", 20], ["b", 30]]

---

<div class="post-metadata">

**Author:** ![Nathan\_Viswa](https://avatars.discourse-cdn.com/v4/letter/n/2bfe46/32.png) [@Nathan\_Viswa](https://rubytalk.org/u/Nathan_Viswa)\
**Post date:** [29 November 2007 14:28 UTC](https://rubytalk.org/t/hash-sorting/42602/4 "2007-11-29T14:28:16Z")

</div>

Thanks Stefano, Jesus for your time and explanation. Makes sense now.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [28 January 2008 17:45 UTC](https://rubytalk.org/t/hash-sorting/42602/5 "2008-01-28T17:45:08Z")

</div>

Nathan Viswa wrote:

> Thanks Stefano, Jesus for your time and explanation. Makes sense now.

Hi,

I'm also trying to sort a hash, but don't succeed for some reason.

My hash is build like this:

hash['key1'] = array\_of\_database\_records\_type\_1  
hash['key2'] = array\_of\_database\_records\_type\_2  
hash['key3'] = array\_of\_database\_records\_type\_3  
hash['key4'] = array\_of\_database\_records\_type\_4

..  
..

and so on. The hash's data values are arrays of rails active record  
objects,  
receifed from a find(:all) call, although their type shouldn't be of  
importance when sorting by key, as I do:

sorted\_hash\_array = hash.sort

The array I receive is not sorted by keys at all, in fact its the same  
array I get when I call hash.to\_a without any sorting.

Sitting on this 'simple' problem for a few hours now, having no idea  
what's the problem.

Any ideas?

Nico

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Tucker](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@James\_Tucker](https://rubytalk.org/u/James_Tucker)\
**Post date:** [28 January 2008 18:44 UTC](https://rubytalk.org/t/hash-sorting/42602/6 "2008-01-28T18:44:47Z")

</div>

hash.to\_a.sort

> **···**
>
> On 28 Jan 2008, at 17:45, Nico Ritsche wrote:
> 
> > Any ideas?

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [28 January 2008 19:40 UTC](https://rubytalk.org/t/hash-sorting/42602/7 "2008-01-28T19:40:19Z")

</div>

James Tucker wrote:

> **···**
>
> > On 28 Jan 2008, at 17:45, Nico Ritsche wrote:
> > 
> > > Any ideas?
> > 
> > hash.to\_a.sort
> 
> Tried that, the result is the same, no sorting is happening at all.  
> Same as if I just do hash.to\_a.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ilan\_Berci](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@Ilan\_Berci](https://rubytalk.org/u/Ilan_Berci)\
**Post date:** [28 January 2008 21:31 UTC](https://rubytalk.org/t/hash-sorting/42602/8 "2008-01-28T21:31:30Z")

</div>

Nico Ritsche wrote:

> James Tucker wrote:
> 
> > > Any ideas?
> > 
> > hash.to\_a.sort
> 
> Tried that, the result is the same, no sorting is happening at all.  
> Same as if I just do hash.to\_a.

James is correct, the array will be sorted but the hash will remain  
"unsorted". Hashes are random access unsorted collections.. you can't  
"sort" a hash..

you can however as James suggested, move the elements to a hash and then  
sort the result any way you want..

irb(main):001:0\> h = {1=\>2,3=\>4,5=\>6}  
=\> {5=\>6, 1=\>2, 3=\>4}  
irb(main):002:0\> h.to\_a.sort  
=\> [[1, 2], [3, 4], [5, 6]]  
irb(main):003:0\> h.to\_a.sort\_by {|a,b| b}  
=\> [[1, 2], [3, 4], [5, 6]]  
irb(main):004:0\>

> **···**
>
> > > On 28 Jan 2008, at 17:45, Nico Ritsche wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ilan\_Berci](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@Ilan\_Berci](https://rubytalk.org/u/Ilan_Berci)\
**Post date:** [28 January 2008 21:32 UTC](https://rubytalk.org/t/hash-sorting/42602/9 "2008-01-28T21:32:43Z")

</div>

Ilan Berci wrote:

> you can however as James suggested, move the elements to a hash and then  
> sort the result any way you want..

errrr.. I mean move the elements to an array.. sorry for the confusion

ilan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [28 January 2008 21:39 UTC](https://rubytalk.org/t/hash-sorting/42602/10 "2008-01-28T21:39:08Z")

</div>

Ilan Berci wrote:

> Ilan Berci wrote:
> 
> > you can however as James suggested, move the elements to a hash and then  
> > sort the result any way you want..
> 
> errrr.. I mean move the elements to an array.. sorry for the confusion
> 
> ilan

Well, yea, but that is what I did, with no success. The hash.sort  
function  
simply calls .to\_a and then sort on the generated array, so it does  
exactly what you and James suggested. Unfortunately the array is not  
sorted when I iterate over it using

array.each do  
...  
end

Is it because iterating like this doesn't preserve the order of array  
elements? I assumed .each do iterates in element order. I will test  
this...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [28 January 2008 21:41 UTC](https://rubytalk.org/t/hash-sorting/42602/11 "2008-01-28T21:41:13Z")

</div>

Ilan Berci wrote:

> ilan

irb(main):001:0\> h = {1=\>2,3=\>4,5=\>6}  
=\> {5=\>6, 1=\>2, 3=\>4}  
irb(main):002:0\> h.to\_a.sort  
=\> [[1, 2], [3, 4], [5, 6]]  
irb(main):003:0\> h.to\_a.sort\_by {|a,b| b}  
=\> [[1, 2], [3, 4], [5, 6]]  
irb(main):004:0\>

h.to\_a.sort\_by {|a,b| b} #sorts by value; the OP wants to sort by key.  
h.to\_a.sort\_by {|key,value| key}  
#or just  
h.to\_a.sort\_by {|key|key}

Regards,

Siep

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [28 January 2008 21:53 UTC](https://rubytalk.org/t/hash-sorting/42602/12 "2008-01-28T21:53:32Z")

</div>

Siep Korteling wrote:

> Ilan Berci wrote:
> 
> > ilan
> 
> irb(main):001:0\> h = {1=\>2,3=\>4,5=\>6}  
> =\> {5=\>6, 1=\>2, 3=\>4}  
> irb(main):002:0\> h.to\_a.sort  
> =\> [[1, 2], [3, 4], [5, 6]]  
> irb(main):003:0\> h.to\_a.sort\_by {|a,b| b}  
> =\> [[1, 2], [3, 4], [5, 6]]  
> irb(main):004:0\>
> 
> h.to\_a.sort\_by {|a,b| b} #sorts by value; the OP wants to sort by key.  
> h.to\_a.sort\_by {|key,value| key}  
> #or just  
> h.to\_a.sort\_by {|key|key}
> 
> Regards,
> 
> Siep

array.sort sorts by key, I know that. That's what I'm trying to do, but  
it doesnt work in my case for some reason

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [28 January 2008 22:02 UTC](https://rubytalk.org/t/hash-sorting/42602/13 "2008-01-28T22:02:06Z")

</div>

Nico Ritsche wrote:

> Siep Korteling wrote:
> 
> > Ilan Berci wrote:
> > 
> > > ilan
> > 
> > irb(main):001:0\> h = {1=\>2,3=\>4,5=\>6}  
> > =\> {5=\>6, 1=\>2, 3=\>4}  
> > irb(main):002:0\> h.to\_a.sort  
> > =\> [[1, 2], [3, 4], [5, 6]]  
> > irb(main):003:0\> h.to\_a.sort\_by {|a,b| b}  
> > =\> [[1, 2], [3, 4], [5, 6]]  
> > irb(main):004:0\>
> > 
> > h.to\_a.sort\_by {|a,b| b} #sorts by value; the OP wants to sort by key.  
> > h.to\_a.sort\_by {|key,value| key}  
> > #or just  
> > h.to\_a.sort\_by {|key|key}
> > 
> > Regards,
> > 
> > Siep
> 
> array.sort sorts by key, I know that. That's what I'm trying to do, but  
> it doesnt work in my case for some reason

maybe this excerpt of my code helps to clarify what I'm trying to do  
(for those who know rails):

@parent\_items = {}

@parent\_items['teachers'] = Teacher.find(:all)  
@parent\_items['prices'] = Price.find(:all)  
@parent\_items['general\_event\_types'] = GeneralEventType.find(:all,  
:conditions =\> "language\_parent\_id = id")  
@parent\_items['specific\_event\_types'] = SpecificEventType.find(:all,  
:conditions =\> "language\_parent\_id = id")

pi\_array\_sorted = @parent\_items.to\_a.sort

and the i like to iterate over the elements of the sorted array, so the  
result should look like this:

pi\_array\_sorted =  
{ 'general\_event\_types', [GeneralEventType1, GeneralEventType2, ..],  
&nbsp;&nbsp;'prices', [Price1, Price2, ..],  
&nbsp;&nbsp;'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
&nbsp;&nbsp;'general\_event\_types', [Teacher1, Teacher2, ..] }

So the keys should be in alphabetical order, but they arent, at least  
not when I iterate over the array using

pi\_array\_sorted.each do |a|  
..  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [28 January 2008 22:26 UTC](https://rubytalk.org/t/hash-sorting/42602/14 "2008-01-28T22:26:30Z")

</div>

Nico Ritsche wrote:

> result should look like this:
> 
> pi\_array\_sorted =  
> { 'general\_event\_types', [GeneralEventType1, GeneralEventType2, ..],  
> &nbsp;&nbsp;'prices', [Price1, Price2, ..],  
> &nbsp;&nbsp;'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> &nbsp;&nbsp;'general\_event\_types', [Teacher1, Teacher2, ..] }
> 
> So the keys should be in alphabetical order, but they arent, (...)

Well, they are, if the last 'general\_event\_types' is substituted with  
'teachers', what it probably should be.  
I guess you want the values sorted.

I don't now Rails, but hey, it's ruby. You can probably do

@parent\_items['teachers'] = Teacher.find(:all).sort

in your second and following lines. Or:

pi\_array\_sorted = @parent\_items.sort.by{|key, value|value.sort!;key}

to have your subarrays sorted and the keys as well in one go.

Regards,

Siep

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rodrigo\_Kochenburger](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@Rodrigo\_Kochenburger](https://rubytalk.org/u/Rodrigo_Kochenburger)\
**Post date:** [29 January 2008 11:29 UTC](https://rubytalk.org/t/hash-sorting/42602/15 "2008-01-29T11:29:59Z")

</div>

> pi\_array\_sorted =  
> { 'general\_event\_types', [GeneralEventType1, GeneralEventType2, ..],  
> 'prices', [Price1, Price2, ..],  
> 'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> 'general\_event\_types', [Teacher1, Teacher2, ..] }
> 
> So the keys should be in alphabetical order, but they arent, at least  
> not when I iterate over the array using

This code above doesn't create an array, it creates an Hash, and Hash  
doesn't preserve order.  
Do the following:

pi\_array\_sorted = [  
&nbsp;&nbsp;['general\_event\_types', [GeneralEventType1,  
GeneralEventType2, ..]],  
&nbsp;&nbsp;['prices', [Price1, Price2, ..]],  
&nbsp;&nbsp;['specific\_event\_types', [SpecificEventType1,  
SpecificEventType2, ..]],  
&nbsp;&nbsp;['general\_event\_types', [Teacher1, Teacher2, ..]]  
]

pi\_array\_sorted.each do |key,value|  
&nbsp;&nbsp;# do stuff here  
end

Cheers

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [29 January 2008 06:55 UTC](https://rubytalk.org/t/hash-sorting/42602/16 "2008-01-29T06:55:40Z")

</div>

Siep Korteling wrote:

> Nico Ritsche wrote:
> 
> > result should look like this:
> > 
> > pi\_array\_sorted =  
> > { 'general\_event\_types', [GeneralEventType1, GeneralEventType2, ..],  
> > &nbsp;&nbsp;'prices', [Price1, Price2, ..],  
> > &nbsp;&nbsp;'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> > &nbsp;&nbsp;'general\_event\_types', [Teacher1, Teacher2, ..] }
> > 
> > So the keys should be in alphabetical order, but they arent, (...)
> 
> Well, they are, if the last 'general\_event\_types' is substituted with  
> 'teachers', what it probably should be.  
> I guess you want the values sorted.
> 
> I don't now Rails, but hey, it's ruby. You can probably do
> 
> @parent\_items['teachers'] = Teacher.find(:all).sort
> 
> in your second and following lines. Or:
> 
> pi\_array\_sorted = @parent\_items.sort.by{|key, value|value.sort!;key}
> 
> to have your subarrays sorted and the keys as well in one go.
> 
> Regards,
> 
> Siep

Hi Siep, yeah, that was a typo, the last general\_event\_types should be  
'teachers'.

But nevertheless, this is NOT what I get, its what I like to get!  
And I really only want to sort by key, as I explained erlier...

Thanks,  
Nico

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [29 January 2008 16:13 UTC](https://rubytalk.org/t/hash-sorting/42602/17 "2008-01-29T16:13:19Z")

</div>

Rodrigo Kochenburger wrote:

> > pi\_array\_sorted =  
> > { 'general\_event\_types', �[GeneralEventType1, GeneralEventType2, ..],  
> > � 'prices', � � � � � � � [Price1, Price2, ..],  
> > � 'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> > � 'general\_event\_types', �[Teacher1, Teacher2, ..] }
> > 
> > So the keys should be in alphabetical order, but they arent, at least  
> > not when I iterate over the array using
> 
> This code above doesn't create an array, it creates an Hash, and Hash  
> doesn't preserve order.  
> Do the following:
> 
> pi\_array\_sorted = [  
> &nbsp;&nbsp;['general\_event\_types', [GeneralEventType1,  
> GeneralEventType2, ..]],  
> &nbsp;&nbsp;['prices', [Price1, Price2, ..]],  
> &nbsp;&nbsp;['specific\_event\_types', [SpecificEventType1,  
> SpecificEventType2, ..]],  
> &nbsp;&nbsp;['general\_event\_types', [Teacher1, Teacher2, ..]]  
> ]
> 
> pi\_array\_sorted.each do |key,value|  
> &nbsp;&nbsp;# do stuff here  
> end
> 
> Cheers

Hi Rodrigo,

sorry, maybe the code was confusing, I just wanted to visualize the  
result array I like to get after sorting. So the "pi\_array\_sorted ="  
shouldn't be an assignement and I used the wrong brakets. Thanks all  
for your comments, but unfortunately concerning  
the sorting issue I'm not a single step further.

Cheers,  
Nico

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ilan\_Berci](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@Ilan\_Berci](https://rubytalk.org/u/Ilan_Berci)\
**Post date:** [29 January 2008 21:39 UTC](https://rubytalk.org/t/hash-sorting/42602/18 "2008-01-29T21:39:43Z")

</div>

Nico Ritsche wrote:

> Rodrigo Kochenburger wrote:
> 
> > > pi\_array\_sorted =  
> > > { 'general\_event\_types', �[GeneralEventType1, GeneralEventType2, ..],  
> > > � 'prices', � � � � � � � [Price1, Price2, ..],  
> > > � 'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> > > � 'general\_event\_types', �[Teacher1, Teacher2, ..] }
> 
> Nico

Nico,

We can help but you must strive to explain what you want clearly and  
legibly.. For the pi\_array\_sorted, it appears that you want to convert a  
hash of arrays into an array of arrays where the second arrays is  
sorted.

Here is a solution that sorts the nested arrays and then the initial  
array

irb(main):002:0\> a = {1=\>[3,7,1,7], 2=\>[3,7,1,1],3=\>[3,2,2,9]}  
=\> {1=\>[3, 7, 1, 7], 2=\>[3, 7, 1, 1], 3=\>[3, 2, 2, 9]}  
irb(main):003:0\> a.each {|b,c| c.sort!}  
=\> {1=\>[1, 3, 7, 7], 2=\>[1, 1, 3, 7], 3=\>[2, 2, 3, 9]}  
irb(main):004:0\> a.sort  
=\> [[1, [1, 3, 7, 7]], [2, [1, 1, 3, 7]], [3, [2, 2, 3, 9]]]

hth

If it doesn't then write the code EXACTLY as you have it.. and then the  
exact output that you desire and we will code it for you.. The code that  
you submitted is barely readable and leaves a lot open to  
interpretation.

ilan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nico\_Ritsche](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@Nico\_Ritsche](https://rubytalk.org/u/Nico_Ritsche)\
**Post date:** [30 January 2008 06:39 UTC](https://rubytalk.org/t/hash-sorting/42602/19 "2008-01-30T06:39:27Z")

</div>

Ilan Berci wrote:

> Nico Ritsche wrote:
> 
> > Rodrigo Kochenburger wrote:
> > 
> > > > pi\_array\_sorted =  
> > > > { 'general\_event\_types', �[GeneralEventType1, GeneralEventType2, ..],  
> > > > � 'prices', � � � � � � � [Price1, Price2, ..],  
> > > > � 'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> > > > � 'general\_event\_types', �[Teacher1, Teacher2, ..] }
> > 
> > Nico
> 
> Nico,
> 
> We can help but you must strive to explain what you want clearly and  
> legibly.. For the pi\_array\_sorted, it appears that you want to convert a  
> hash of arrays into an array of arrays where the second arrays is  
> sorted.
> 
> Here is a solution that sorts the nested arrays and then the initial  
> array
> 
> irb(main):002:0\> a = {1=\>[3,7,1,7], 2=\>[3,7,1,1],3=\>[3,2,2,9]}  
> =\> {1=\>[3, 7, 1, 7], 2=\>[3, 7, 1, 1], 3=\>[3, 2, 2, 9]}  
> irb(main):003:0\> a.each {|b,c| c.sort!}  
> =\> {1=\>[1, 3, 7, 7], 2=\>[1, 1, 3, 7], 3=\>[2, 2, 3, 9]}  
> irb(main):004:0\> a.sort  
> =\> [[1, [1, 3, 7, 7]], [2, [1, 1, 3, 7]], [3, [2, 2, 3, 9]]]
> 
> hth
> 
> If it doesn't then write the code EXACTLY as you have it.. and then the  
> exact output that you desire and we will code it for you.. The code that  
> you submitted is barely readable and leaves a lot open to  
> interpretation.
> 
> ilan

Hi Ilan,

actually I supplied my exact code already.

So here again, my exact code that doesn't work:

@parent\_items = {}

@parent\_items['teachers'] = Teacher.find(:all)  
@parent\_items['prices'] = Price.find(:all)  
@parent\_items['general\_event\_types'] = GeneralEventType.find(:all,  
:conditions =\> "language\_parent\_id = id")  
@parent\_items['specific\_event\_types'] = SpecificEventType.find(:all,  
:conditions =\> "language\_parent\_id = id")

pi\_array\_sorted = @parent\_items.to\_a.sort

So what I'm doing here is building a hash containing arrays of rails  
active record objects. So for example @parent\_items['teachers'] is an  
array of Teacher records. Then I call the hashs's sort method. After  
doing this I expect pi\_array\_sorted to be a nested array, sorted by key,  
as the documentation of Hash.sort suggests ("Converts hsh to a nested  
array of [key, value] arrays and sorts it, using Array#sort."). So the  
result for the above code should be this nested array:

['general\_event\_types', [GeneralEventType1, GeneralEventType2, ..],  
&nbsp;&nbsp;'prices', [Price1, Price2, ..],  
&nbsp;&nbsp;'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
&nbsp;&nbsp;'teachers', [Teacher1, Teacher2, ..] ]

But this is not what I get. The keys in the resulting array still have  
an arbitrary order, the exact same order as if I don't call .sort at  
all.

Nico

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [30 January 2008 06:45 UTC](https://rubytalk.org/t/hash-sorting/42602/20 "2008-01-30T06:45:55Z")

</div>

Can you cut, paste, and post the snippit of code you are using to display your incorrect results?

> **···**
>
> On Jan 30, 2008, at 1:39 AM, Nico Ritsche wrote:
> 
> > ['general\_event\_types', [GeneralEventType1, GeneralEventType2, ..],  
> > &nbsp;&nbsp;'prices', [Price1, Price2, ..],  
> > &nbsp;&nbsp;'specific\_event\_types', [SpecificEventType1, SpecificEventType2, ..],  
> > &nbsp;&nbsp;'teachers', [Teacher1, Teacher2, ..] ]
> > 
> > But this is not what I get. The keys in the resulting array still have  
> > an arbitrary order, the exact same order as if I don't call .sort at  
> > all.

[Next page](https://rubytalk.org/t/hash-sorting/42602.md?page=2)
