Sorting a collection of objects by one or more attributes
Ruby
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION

README.rdoc

attributes_sort

This gem allows you to take a collection of any type of object and sort them using the attributes of the object.

Example:

require 'rubygems'
require 'attributes_sort'

class Person
  include AttributesSort

  attr_accessor :firstname,:lastname,:age

  def initialize(firstname="", lastname="" ,age=0)
    @firstname = firstname
    @lastname = lastname
    @age = 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 people.attr_sort(:sort_by=>[:lastname])
p people.attr_sort(:sort_by=>[:age])
p people.attr_sort(:sort_by=>[:lastname,:firstname,:age])

Copyright

Copyright © 2009 Karmen Blake. See LICENSE for details.