From: "Peña, Botp" Date: 2007-05-09T13:45:21+09:00 Subject: Re: Implementation of the object.sort method. On Behalf Of Jorge Domenico Bucaran Romano: # [3,2,1,4].sort do |a,b| # a <=> b # end botp@pc4all:~$ qri array#sort ------------------------------------------------------------- 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"] and irb(main):023:0> a => ["d", "a", "e", "c", "b"] irb(main):024:0> a.sort do |x,y| irb(main):025:1* case irb(main):026:2* when x == "b" irb(main):027:2> -1 irb(main):028:2> when x == "c" irb(main):029:2> 1 irb(main):030:2> else irb(main):031:2* x <=> y irb(main):032:2> end irb(main):033:1> end => ["b", "a", "d", "e", "c"] irb(main):034:0> here we are giving special treatments to "b" and "c", where "b" always come first in comparison while "c" always come last. The rest of the elements follow the normal comparison using the <=> op. # I know it will sort my array but I don't understand the way # this method # works because other tests I have made bring none results. pls post problem codes/results so everyone may help. kind regards -botp