From: Martin DeMello Date: 2003-08-07T07:43:54+09:00 Subject: Re: What's New and Shiny in Ruby 1.8.0? Harry Ohlsen wrote: > but I can't think of a way to get it to work for strings, for example. Maybe > if strings had a unary minus that changed every character to the character > with code (255 - ascii-code) ... but that sounds like a real hack :-). Of > course, it's not general enough, either. Still hacky, but general: class Test attr_accessor :foo, :bar def initialize(f, b) @foo, @bar = f, b end def inspect "[#{foo}, #{bar}]" end end def rev(obj) a = obj.dup class << a alias old_cmp <=> def <=>(other) -old_cmp(other) end end a end a = [ Test.new("Hello", 1), Test.new("World", -1), Test.new("Foo", 5), Test.new("bar", 4) ] p a p a.sort_by {|x| x.foo} p a.sort_by {|x| rev(x.foo)} martin