开发者

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 }
 @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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜