Sort with block
require 'set'
w = Set['apple','Beet','carrot'] # A set of words to sort
w.sort # ['Beet','apple','carrot']: alphabetical
w.sort {|a,b| b<=>a } # ['carrot','apple','Beet']: reverse
w.sort {|a,b| a.casecmp(b) } # ['apple','Beet','carrot']: ignore case
w.sort {|a,b| b.size<=>a.size} # ['carrot','apple','Beet']: reverse length
Related examples in the same category