Is it possible to do conditional sort in hash?
My hash be like:
{1=>"10",2=>"20",3=>"30",4=>"40",5=>"50",6=>"60",7=>"70",8=>"80",9=>"90"}
Desired result is:
{7=>"70",8=>"80",9=>"90",1=>"10",2=>"20",3=>"30",4=>"40",5=>"50",6=>"60"}
add condition in this code
operation_hour_hash.sort{|key,value| key[1]<=>value[1]}
Sure, you could do something like:
But, sorting a hash doesn’t really make much sense. Before the sort actually takes place it’s converted to an array of [key,value] pairs then sorted by -1,0,1 returned from <=>.
If you need sorted hashes you’ll need to use something like RubyFacets Dictionary class.