我有一系列哈希如下
[
{ :foo => 'foo', :bar => 2 },
{ :foo => 'foo', :bar => 3 },
{ :foo => 'foo', :bar => 5 },
]
Run Code Online (Sandbox Code Playgroud)
我试图根据:bar每个哈希值中的降序对数组进行排序.
我正在使用sort_by如下排序数组.
a.sort_by { |h| h[:bar] }
Run Code Online (Sandbox Code Playgroud)
但是,上面按升序对数组进行排序.如何按降序排序?
一种解决方案是执行以下操作:
a.sort_by { |h| -h[:bar] }
Run Code Online (Sandbox Code Playgroud)
但这个负号似乎不合适.任何意见?