Thomas Wieczorek
8/24/2008 12:28:00 PM
Heya!
On Sun, Aug 24, 2008 at 2:03 PM, Adam Akhtar <adamtemporary@gmail.com> 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]
# => [#<Example:0x2b5d6a8 @value=10>, #<Example:0x2b5b7cc @value=0>]
arr.sort!
# => [#<Example:0x2b5b7cc @value=0>, #<Example:0x2b5d6a8 @value=10>],
means arr = [b, a]