Setting up .sort function to your custom class in RUBY


class Eren
include Comparable
attr_accessor :age


def initialize _age
@age = _age
end


def <=> other
self.age <=> other.age
end
end


e1 = Eren.new(10)
e2 = Eren.new(11)
e3 = Eren.new(2)


d = [e1,e2,e3].sort!
d.each do |x|
puts x.age
end

Share