Created
March 7, 2010 20:33
-
-
Save apeiros/324609 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
class ReverseSortProxy | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
def <=>(other) | |
other.value <=> @value | |
end | |
end | |
module Enumerable | |
def directed_sort_by(*dirs) | |
dirs.map! { |x| | |
case x | |
when :asc, :ascending, 1 | |
false | |
when :desc, :descending, -1 | |
true | |
else | |
raise ArgumentError, "Invalid sort order - #{x}" | |
end | |
} | |
sort_by { |x| yield(x).zip(dirs).map { |v,d| | |
d ? ReverseSortProxy.new(v) : v | |
}} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment