A
Adam Akhtar
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.
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.