看一下<=>的行为,
前面大返回 1 后面大返回 -1 相等返回 0
自定义类
前面大返回 1 后面大返回 -1 相等返回 0
>> 1 <=> 2
=> -1
>> 2 <=> 1
=> 1
>> 1 <=> 1
=> 0
自定义类
class Compare
attr_accessor :date
def initialize(date)
@date = date
end
# 按照日期倒序排列
def <=>(other_compare)
other_compare.date <=> self.date
end
end