Returns a new array created by sorting self
.html
Comparisons for the sort will be done using the <=>
operator or using an optional code block.数组
The block must implement a comparison between a
and b
, and return -1
, when a
follows b
, 0
when a
and b
are equivalent, or +1
if b
follows a
.ruby
See also Enumerable#sort_by.ide
a = [ "d", "a", "e", "c", "b" ] a.sort #=> ["a", "b", "c", "d", "e"] a.sort { |x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
a.sort { |x,y| x <=> Y } #=> ["a", "b", "c", "d", "e"]
2.
Sorts self
in place.函数
Comparisons for the sort will be done using the <=>
operator or using an optional code block.ui
The block must implement a comparison between a
and b
, and return -1
, when a
follows b
, 0
when a
and b
are equivalent, or +1
if b
follows a
.spa
See also Enumerable#sort_by.code
a = [ "d", "a", "e", "c", "b" ] a.sort! #=> ["a", "b", "c", "d", "e"] a.sort! { |x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]参考连接:http://www.ruby-doc.org/core-2.0/Array.html#method-i-sort3.sort和sort!的区别:sort和sort!函数,默认都使用 <=>比较,他们的区别在于:sort! 可能会改变原先的数组,因此加个感叹号提醒sort 返回的是新数组,没对原先的数组进行修改在ruby的SDK里,能看到不少加了感叹号的函数,都意味着对函数操做的对象进行了状态更改。