[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

hahses and sort

Adam Akhtar

8/21/2008 2:07:00 PM

i have a hash whose values are hashes themselves like this

stats_hash =
{
"fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0},
"third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
"second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
"first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0}
}

and I want to order the hash in decending order based on the the value
:str

to give

{
"first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0},
"second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
"third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
"fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0}
}



so ive outlined this function

def order_statistics_by_str(some_items_statistics)
some_items_statistics.sort do |one, another|
ones_str = one[:str]
another_str = another[:str]
-ones_str <=> another_str
end
end

however this
ones_str = one[:str] and another_str = another[:str]
is bad syntax

im new to ruby and nested hashes and how to access them is proving to be
trick for me. how do i access what :str refers to.
--
Posted via http://www.ruby-....

4 Answers

Farrel Lifson

8/21/2008 2:35:00 PM

0

2008/8/21 Adam Akhtar <adamtemporary@gmail.com>:
> i have a hash whose values are hashes themselves like this
>
> stats_hash =
> {
> "fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0},
> "third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
> "second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
> "first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0}
> }
>
> and I want to order the hash in decending order based on the the value
> :str
>
> to give
>
> {
> "first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0},
> "second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
> "third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
> "fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0}
> }

In general hashes are not ordered data structures.

Farrel
--
Aimred - Ruby Development and Consulting
http://www....

Lex Williams

8/21/2008 2:44:00 PM

0

> however this
> ones_str = one[:str] and another_str = another[:str]
> is bad syntax
>
> im new to ruby and nested hashes and how to access them is proving to be
> trick for me. how do i access what :str refers to.

state_hash.each_key do |key|
puts state_hash[key][:str]
end

this will output the values
--
Posted via http://www.ruby-....

Tim Hunter

8/21/2008 2:47:00 PM

0

Adam Akhtar wrote:
> i have a hash whose values are hashes themselves like this
>
> stats_hash =
> {
> "fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0},
> "third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
> "second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
> "first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0}
> }
>
> and I want to order the hash in decending order based on the the value
> :str
>
> to give
>
> {
> "first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0},
> "second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
> "third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
> "fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0}
> }
>

Hashes are by definition unordered. The best you can do is make an array
of the hash keys ordered the way you want them, then access the hash
using the array.

stats_hash =
{
"fourth"=> {:total_completed=>1, :total_sold=>1, :str=>0.0},
"third"=> {:total_completed=>1, :total_sold=>0, :str=>25.0},
"second"=>{:total_completed=>1, :total_sold=>0, :str=>50.0},
"first"=>{:total_completed=>1, :total_sold=>0, :str=>90.0}
}

keys = stats_hash.keys
sorted_keys = keys.sort_by {|k| -stats_hash[k][:str]}
puts sorted_keys
--
Posted via http://www.ruby-....

Adam Akhtar

8/22/2008 2:38:00 AM

0

wow thanks for that. I actually found a post which had similiar syntax
ie. hash[1][:str] in the sort block. I couldnt work out why that was
legal syntax for a hash. But as you have just told me, ruby converts the
hash to an array so accessing it like that is fine.

Thank you very much for your help.
--
Posted via http://www.ruby-....