更新时间:2022-03-14 23:10:01
Ruby 的 sort
不能就地排序.(也许你有 Python 背景?)
Ruby's sort
doesn't sort in-place. (Do you have a Python background, perhaps?)
Ruby 有 sort!
用于就地排序,但在 Ruby 1.8 中没有 sort_by
的就地变体.在实践中,你可以这样做:
Ruby has sort!
for in-place sorting, but there's no in-place variant for sort_by
in Ruby 1.8. In practice, you can do:
sorted = sort_me.sort_by { |k| k["value"] }
puts sorted
从 Ruby 1.9+ 开始,.sort_by!
可用于就地排序:
As of Ruby 1.9+, .sort_by!
is available for in-place sorting:
sort_me.sort_by! { |k| k["value"]}