Sort a model object


I have two tables

  • Topic

  • Comments

I have relation of topic and comments defined on database. Right now I am getting all record from the table by simple using

findAll() function. Now i need to sort this data or records on the basis of its number of comments. Like condition should be

"order by DESC count(topic->comments)" where comments is the name of the relation. I have three different filters on which i need to sort so please give a general solution. Filters are

1: Topics with most comments

2: Topics with most comments in last week

3: Latest topics on date of creation (already handled)

Thanks in advance

This link may help you.