开发者

reversing hash.sort_by in ruby

开发者 https://www.devze.com 2023-03-30 08:52 出处:网络
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. Ho

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 }
 @s开发者_JS百科ortedInternalLinksHash = @countHash.sort_by { |k,v| !v }


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


Also you can use:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号