Topic: sorting descending order

Hi, I have an object which I want to sort by popularity (default sort is by date).

I can sort using:

ttssorted = tts.sort do |a,b| a.popularity <=> b.popularity end

Problem is it sorts in ascending order.

I can fix it by doing:

ttsorted.reverse

but that seems like its inefficient, is there a way to sort in descending order on the first try?

thanks
Joel

Re: sorting descending order

just swap the props

ttssorted = tts.sort do |a,b| b.popularity end <=> a.popularity

Re: sorting descending order

You can also do this:

tts.sort_by(&:popularity).reverse

Railscasts - Free Ruby on Rails Screencasts