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:


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


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:


Railscasts - Free Ruby on Rails Screencasts