From: Thomas Wieczorek Date: 2008-08-24T21:27:35+09:00 Subject: Re: defining sort <=> for a class Heya! On Sun, Aug 24, 2008 at 2:03 PM, Adam Akhtar wrote: > i tried this > > class Example > > attr_accessor :value > > def initialize > @value = 0 > end > Here's the error: > def <=>(other_example) > @value <=> other_example > end You want to sort by the value, so you have to compare the values: def <=>(other_example) @value <=> other_example.value end A simple example: a = Example.new b = Example.new a.value = 10 arr = [a,b] # => [#, #] arr.sort! # => [#, #], means arr = [b, a]