Skip to content

Instantly share code, notes, and snippets.

@jherdman
Created December 4, 2009 16:45
Show Gist options
  • Save jherdman/249144 to your computer and use it in GitHub Desktop.
Save jherdman/249144 to your computer and use it in GitHub Desktop.
# A sample class...
class Person
attr_accessor :name, :gender, :born_on
def initialize(name, age, born_on)
self.name = name
self.gender = gender
self.born_on = born_on
end
def <=>(other)
# Even though this is a protected method, members of the same class can
# access each other's protected methods. I bet you thought you'd never
# need to know that!
sortable_attributes <=> other.sortable_attributes
end
protected
def sortable_attributes
[name, gender, born_on]
end
end
# Some data...
p1 = Person.new("Jimmy", :male, "1982-5-16")
p2 = Person.new("Sonya", :female, "1986-2-4")
p3 = Person.new("Beth", :female, "1990-10-4")
p4 = Person.new("Beth", :female, "1970-2-14")
# Now sort!
[p1, p2, p3, p4].sort #=> [p4, p3, p1, p2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment