Easy Implementation of Sorting Code
Helpful functionality for sorting and comparing
Brought to you by:
margotresue
File | Date | Author | Commit |
---|---|---|---|
development | 2013-02-22 |
![]() |
[07a535] Added development/TODO.txt |
doc | 2013-02-23 |
![]() |
[4e23d1] Regenerated yard docs. |
documentation | 2013-02-22 |
![]() |
[f7ca7e] Initial commit. |
lib | 2013-02-23 |
![]() |
[8a8417] Changed how Sorting::Helpers' methods are added... |
rake | 2013-03-02 |
![]() |
[15556c] Added initial versions of "gem" and "test" rake... |
test | 2013-03-02 |
![]() |
[4825f1] Slightly reorganized test internals. |
.gitignore | 2013-02-22 |
![]() |
[80720e] Updated .gitignore. |
.yardopts | 2013-02-22 |
![]() |
[d2c500] Updated yard options. |
LICENSE.txt | 2013-02-22 |
![]() |
[f7ca7e] Initial commit. |
README.markdown | 2013-02-23 |
![]() |
[601010] Changed order of first examples (README and Sor... |
Rakefile | 2013-02-22 |
![]() |
[f7ca7e] Initial commit. |
sorting.gemspec | 2013-02-23 |
![]() |
[7686f0] Cleaned up gemspec and added documentation dir ... |
Helpful functionality for sorting and comparing.
gem install sorting
Sort a list of Person objects
# Convenient
require 'sorting/convenience'
people.sort_by { |person| [asc(person.first_name), asc(person.last_name), desc(person.age)] }
# Or without patching Kernel
require 'sorting'
people.sort_by { |person|
[Sorting.asc(person.first_name), Sorting.asc(person.last_name), Sorting.desc(person.age)]
}
# Care about expensive comparison values which may not always be needed
# assume item.expensive_value takes a lot of time to compute, but since it's the second value,
# it might only be needed in a small number of cases.
require 'sorting/convenience'
items.sort_by { |item|
[asc(item.cheap_value), asc(:nils_last) { item.expensive_value }]
}
# Care about nil values in your data
require 'sorting/convenience'
people.sort_by { |person|
[asc(person.first_name, :nils_first), asc(person.first_name, :nils_last)]
}
# Only care about nil values in your data
[5,3,nil,9].sort_by { |x| x || Sorting::Bigger } # Sorting::Smaller is available too
Take a look at {file:documentation/examples.rb} for more examples.
You can use this code under the {file:LICENSE.txt BSD-2-Clause License}, free of charge.
If you need a different license, please ask the author.