public Method

Array.sort

array.sort                    an_array 
array.sort {| a,b | block }   an_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 implements a comparison between a and b, returning -1, 0, or +1. 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"]

Source Code

/*
*  call-seq:
*     array.sort                   -> an_array 
*     array.sort {| a,b | block }  -> an_array 
*  
*  Returns a new array created by sorting <i>self</i>. Comparisons for
*  the sort will be done using the <code><=></code> operator or using
*  an optional code block. The block implements a comparison between
*  <i>a</i> and <i>b</i>, returning -1, 0, or +1. See also
*  <code>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"]
*/

VALUE
rb_ary_sort(ary)
   VALUE ary;
{
   ary = rb_ary_dup(ary);
   rb_ary_sort_bang(ary);
   return ary;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.