# Help with sorting results

**URL:** <https://www.ruby-forum.com/t/help-with-sorting-results/144581>\
**Category:** Rails\
**Created:** [July 31, 2008, 7:25am UTC](https://www.ruby-forum.com/t/help-with-sorting-results/144581 "2008-07-31T07:25:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [July 31, 2008, 7:25am UTC](https://www.ruby-forum.com/t/help-with-sorting-results/144581/1 "2008-07-31T07:25:05Z")

</div>

Greetings,

I’m programming a section of my application that will store and list  
stories. These stories should be sorted by three factors: genre,  
order\_by (such as time posted), and sort (ascending or descending). I  
want to give my users the ability to easily sort through the list of  
stories.

To achieve this, I’ve setup a named route:

map.sort\_by ‘stories/sort\_by/:genre/:order\_by/:sort’, :controller =\>  
“stories”, :action =\> “index” (Both :order\_by and :sort may be left  
nil)

I’m stuck at the point of actually sorting and returning the results  
(the controller logic). I’ve done some research, though am still quite  
a newbie on rails. \>\< In your opinion, what would be the best way to  
sort the stories based off of what is, and is not supplied to the  
controller? For example, if only params(:genre) is supplied, I want  
the list of stories to only display the specified genre. However, if  
params(:order\_by) is also supplied, the list would be of one genre and  
listed, ASC or DESC by whatever :order\_by is targeting.

Thanks in advance for you help!

~Dustin T.

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [July 31, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/help-with-sorting-results/144581/2 "2008-07-31T18:48:39Z")

</div>

> nil)
> 
> I’m stuck at the point of actually sorting and returning the results  
> (the controller logic). I’ve done some research, though am still quite  
> a newbie on rails. \>\< In your opinion, what would be the best way to  
> sort the stories based off of what is, and is not supplied to the  
> controller? For example, if only params(:genre) is supplied, I want  
> the list of stories to only display the specified genre. However, if  
> params(:order\_by) is also supplied, the list would be of one genre and  
> listed, ASC or DESC by whatever :order\_by is targeting.

I’d do something like this:

if params[:genre]  
conditions = [‘genre = ?’, params[:genre]  
else  
conditions = nil  
end

params[:sort] = ‘asc’ unless [‘asc’, ‘desc’].include?  
(params[:sort].downcase)

params[:order] = ‘title’ unless [‘title’, ‘created\_at’,  
‘some\_field’].include?(params[:order\_by].downcase)

Story.find(:all, :conditions =\> conditions, :order =\>  
“#{params[:order]} #{params[:sort]}”)

This way you check all the inputs and force them into something  
reasonable if they aren’t what you want.

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [July 31, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/help-with-sorting-results/144581/3 "2008-07-31T21:03:22Z")

</div>

Thanks Philip!

That really helped. The application is running smoothly and everything  
is working. 🙂

~Dustin T.
