On Oct 13, 2010, at 02:08 , Y. NOBUOKA wrote:

> The one way to do a reverse sort is wrapping a sort target in a class
> for a reverse sort.
> For instance:
> 
> # This class is a class for a reverse sort.
> # An Object of various datatypes can be wrapped in an object of this class.
> class ItemForReverseSort
>  def initialize( item )
>    @item = item
>  end
>  def item
>    @item
>  end
>  def <=>( target )
>    ( self.item <=> target.item ) * (-1)
>  end
> end

I like this, but think it is more complex than it needs to be.

module ReverseSort
  def <=> target
    -super
  end
end

class Object
  def -@
    self.dup.extend ReverseSort
  end
end

b = [ ["radio", 30, 5], ["radio", 20, 5], ["archie", 20, 5], ["newton", 10, 3] ]

p b.sort_by{|x| [ x[1], -x[0] ]}
# => [["newton", 10, 3], ["radio", 20, 5], ["archie", 20, 5], ["radio", 30, 5]]