Method: Array#sort
- Defined in:
- array.c
#sort ⇒ Object #sort {|a, b| ... } ⇒ Object
Returns a new array containing the elements of self
, sorted.
With no block given, compares elements using operator #<=>
(see Object#<=>):
[0, 2, 3, 1].sort # => [0, 1, 2, 3]
With a block given, calls the block with each combination of pairs of elements from self
; for each pair a
and b
, the block should return a numeric:
-
Negative when
b
is to followa
. -
Zero when
a
andb
are equivalent. -
Positive when
a
is to followb
.
Example:
a = [3, 2, 0, 1]
a.sort {|a, b| a <=> b } # => [0, 1, 2, 3]
a.sort {|a, b| b <=> a } # => [3, 2, 1, 0]
When the block returns zero, the order for a
and b
is indeterminate, and may be unstable.
Related: see Methods for Fetching.
3480 3481 3482 3483 3484 3485 3486 |
# File 'array.c', line 3480
VALUE
rb_ary_sort(VALUE ary)
{
ary = rb_ary_dup(ary);
rb_ary_sort_bang(ary);
return ary;
}
|