Created
October 25, 2009 23:59
-
-
Save kblake/218334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sortable | |
def sort(options={}) | |
attributes = options[:attributes].inject("["){|attribute_string, attribute| attribute_string << "object.#{attribute},"} + "]" | |
options[:collection].sort_by{|object| eval(attributes)} | |
end | |
end | |
class Person | |
extend Sortable | |
attr_accessor :firstname,:lastname,:age | |
def initialize(firstname,lastname,age=0) | |
@firstname = firstname | |
@lastname = lastname | |
@age = age | |
end | |
def name | |
"#{@firstname} #{@lastname}" | |
end | |
def to_s | |
"Hello my name is #{name} and my age is #{@age}" | |
end | |
end | |
people = [Person.new("joe","blow",12),Person.new("joe","blow",89),Person.new("mary","watson",32),Person.new("annie","watson",9),Person.new("bob","builder",12)] | |
p Person.sort(:collection=>people,:attributes=>[:lastname]) | |
p Person.sort(:collection=>people,:attributes=>[:age]) | |
p Person.sort(:collection=>people,:attributes=>[:lastname,:firstname,:age]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment