본문 바로가기

Ruby & Rails

[Ruby] hash, sort by value, sort by key


r=Hash.new()
sorted_r = r.sort {|a,b| a[1] <=> b[1]}
sorted_r = r.sort {|a,b| -(a[1] <=> b[1]} #reverse
r.sort{|k,v| k}
r.sort{|k,v| v}
view raw sort.rb hosted with ❤ by GitHub