Method: Array#sort
- Defined in:
- array.c
#sort ⇒ Array #sort {|a, b| ... } ⇒ Array
Returns a new array created by sorting self.
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.
See also Enumerable#sort_by.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort { |x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
2506 2507 2508 2509 2510 2511 2512 |
# File 'array.c', line 2506
VALUE
rb_ary_sort(VALUE ary)
{
ary = rb_ary_dup(ary);
rb_ary_sort_bang(ary);
return ary;
}
|