and I have an array of examples and want to sort them based on value how
do i define a <=> for my class. I know its possible as it was a side
comment on an earlier post. Seeing as this is a totally different
subject to that post i decided to ask in a new post - it will be easier
for others who may have the same questoin to search for.
i tried this
class Example
attr_accessor :value
def initialize @value = 0
end
def <=>(other_example) @value <=> other_example
end
end
but i keep getting an error of undefined method for nill class when i
try to call it like this with an array of example objects
ojects.sort do |one, another|
one.<=>(another)
end
i also tried
ojects.sort do |one, another|
one <=>(another)
end
.sort_by{|r| r.name.downcase } # ascibetic order by name
Actually, sorting by a downcase is not strictly an asciibetic sort. It’s
a cheap
form of “collation”, where an expensive collation uses your locale to
decipher
its dictionary order rules. Example: Packing McDonalds in with
MacDonalds…