since symbols do not respond to the <=> method used by sort, does anyone have a technique to sorting an array of symbols? interested in seeing some other ideas.
Well, symbols.sort_by {|sym| sym.to_s}
works.
Also in 1.9 symbols do respond to <=>
, so there you can just do symbols.sort
.
If you want to work on older rubies as though they were 1.9 you can just define <=> on Symbol
class Symbol
include Comparable
def <=>(other)
self.to_s <=> other.to_s
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With