Sort an enumerable in descending order
What's the best way to sort an Enumerable
in descending order?
I've been doing @array.sort.reverse
or @array.sort_by{|song| song.title }.reverse
I suppo开发者_开发问答se I could do something like @array.sort{|a, b| b.title <=> a.title}
, but I find this hard to read and verbose.
The performance of Array.reverse
is not very bad. What costs you by using @array.sort.reverse
is an extra array duplication plus the reverse (n/2 element switches). So yes, I think that should be acceptable if you think it's read clearer.
See its source for details. And also, I think using @array.sort.reverse
does provide 'slightly' better readability (but it's not very hard to read any way).
I'm not sure whether this works any better than Wayne Conrad's self-described "obvious piece of junk," but you could define Enumerable#sort_by_descending
as
Enumerable.class_eval do
def sort_by_descending(&block)
sort { |a, b| block.bind(b).call <=> block.bind(a).call }
end
end
Then call it as follows:
@songs.sort_by_descending(&:title)
精彩评论