overtone overtone - 1 year ago 76
Ruby Question

reversing hash.sort_by in ruby

Simple enough but Im drawing a blank on it.

@sortedHash = @otherHash.sort_by { |k,v| v }

This stores the has based on key value in ascending order. in other words first value in hash is lowest. How do i reverse (descending order) it so that the highest value is at the top?

@sortedInternalLinksHash = @countHash.sort_by { |k,v| -v }
@sortedInternalLinksHash = @countHash.sort_by { |k,v| !v }

Answer Source
@sortedInternalLinksHash = @countHash.sort_by { |k,v| v }.reverse
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download