instance method
Array#sort_by!
[U] () { (Elem obj) -> U } -> ::Array[Elem]
() -> ::Enumerator[Elem, ::Array[Elem]]
sort_by! {|element| ... } → self
sort_by! → new_enumerator
array.c
View on GitHub ↗
static VALUE
rb_ary_sort_by_bang(VALUE ary)
{
VALUE sorted;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
if (RARRAY_LEN(ary) > 1) {
sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
rb_ary_replace(ary, sorted);
}
return ary;
}
With a block given, sorts the elements of self in place; returns self.
Calls the block with each successive element; sorts elements based on the values returned from the block:
Example
a = ['aaaa', 'bbb', 'cc', 'd'] a.sort_by! {|element| element.size } a # => ["d", "cc", "bbb", "aaaa"]
For duplicate values returned by the block, the ordering is indeterminate, and may be unstable.
With no block given, returns a new Enumerator.
Related: see Methods for Assigning.