From: Phrogz Date: 2007-05-09T14:00:15+09:00 Subject: Re: Implementation of the object.sort method. On May 8, 9:54 pm, Jorge Domenico Bucaran Romano wrote: > Can you show me an implementation of the Array.sort method? Slim:~ gkistner$ cd /usr/local/src/ruby-1.8.5-p12/ Slim:/usr/local/src/ruby-1.8.5-p12 gkistner$ cat array.c [...snip...] VALUE rb_ary_sort(ary) VALUE ary; { ary = rb_ary_dup(ary); rb_ary_sort_bang(ary); return ary; } [...snip...] VALUE rb_ary_sort_bang(ary) VALUE ary; { rb_ary_modify(ary); if (RARRAY(ary)->len > 1) { FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */ rb_ensure(sort_internal, ary, sort_unlock, ary); } return ary; } [...snip...] static VALUE sort_internal(ary) VALUE ary; { struct ary_sort_data data; data.ary = ary; data.ptr = RARRAY(ary)->ptr; data.len = RARRAY(ary)->len; qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE), rb_block_given_p()?sort_1:sort_2, &data); return ary; } [...snip...] static int sort_1(a, b, data) VALUE *a, *b; struct ary_sort_data *data; { VALUE retval = rb_yield_values(2, *a, *b); int n; n = rb_cmpint(retval, *a, *b); ary_sort_check(data); return n; } There's your implementation. I suspect that's not what you wanted. Could you try asking your question again using different words? Perhaps then I will understand how to help you. Does it help if you know what the Quicksort[1] algorithm is? [1] http://en.wikipedia.org/wiki/Quicksort