Velocity Reviews > Ruby > Hash Sorting

# Hash Sorting

Siep Korteling
Guest
Posts: n/a

 01-28-2008
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/.

Nico Ritsche
Guest
Posts: n/a

 01-28-2008
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/.

Nico Ritsche
Guest
Posts: n/a

 01-28-2008
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, ..],
'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

pi_array_sorted.each do |a|
..
end

--
Posted via http://www.ruby-forum.com/.

Siep Korteling
Guest
Posts: n/a

 01-28-2008
Nico Ritsche wrote:
> result should look like this:
>
> 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, (...)

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/.

Nico Ritsche
Guest
Posts: n/a

 01-29-2008
Siep Korteling wrote:
> Nico Ritsche wrote:
>> result should look like this:
>>
>> 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, (...)

>
> 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/.

Rodrigo Kochenburger
Guest
Posts: n/a

 01-29-2008
> 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 = [
['general_event_types', [GeneralEventType1,
GeneralEventType2, ..]],
['prices', [Price1, Price2, ..]],
['specific_event_types', [SpecificEventType1,
SpecificEventType2, ..]],
['general_event_types', [Teacher1, Teacher2, ..]]
]

pi_array_sorted.each do |key,value|
# do stuff here
end

Cheers

Nico Ritsche
Guest
Posts: n/a

 01-29-2008
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 = [
> ['general_event_types', [GeneralEventType1,
> GeneralEventType2, ..]],
> ['prices', [Price1, Price2, ..]],
> ['specific_event_types', [SpecificEventType1,
> SpecificEventType2, ..]],
> ['general_event_types', [Teacher1, Teacher2, ..]]
> ]
>
> pi_array_sorted.each do |key,value|
> # 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
the sorting issue I'm not a single step further.

Cheers,
Nico

--
Posted via http://www.ruby-forum.com/.

Ilan Berci
Guest
Posts: n/a

 01-29-2008
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/.

Nico Ritsche
Guest
Posts: n/a

 01-30-2008
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, ..],
'prices', [Price1, Price2, ..],
'specific_event_types', [SpecificEventType1, SpecificEventType2, ..],
'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/.

Gary Wright
Guest
Posts: n/a

 01-30-2008

On Jan 30, 2008, at 1:39 AM, Nico Ritsche wrote:
>
>
> [ 'general_event_types', [GeneralEventType1, GeneralEventType2, ..],
> 'prices', [Price1, Price2, ..],
> 'specific_event_types', [SpecificEventType1,
> SpecificEventType2, ..],
> '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.

Can you cut, paste, and post the snippit of code you are using to