# Custom Sort - Possible?

**URL:** <https://www.ruby-forum.com/t/custom-sort-possible/147646>\
**Category:** Rails\
**Created:** [September 3, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/custom-sort-possible/147646 "2008-09-03T23:11:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [September 3, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/custom-sort-possible/147646/1 "2008-09-03T23:11:40Z")

</div>

Hi all,

I’m working on a help system that lets users assign a “tag color” to  
their requests in addition to assigning the usual “high, medium, low”  
priority. My question is whether it’s possible to do a sort based on  
the tag color. Valid colors are green, yellow, orange, and red, and  
these are stored as strings in the database.

I’d like to use something like:

# Customer Model

has\_many :requests, :order =\> priority, tag\_color

Is it possible to add a sort based on these string values?

Thanks!

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [September 4, 2008, 12:04am UTC](https://www.ruby-forum.com/t/custom-sort-possible/147646/2 "2008-09-04T00:04:54Z")

</div>

> I’m working on a help system that lets users assign a “tag color” to  
> their requests in addition to assigning the usual “high, medium, low”  
> priority. My question is whether it’s possible to do a sort based on  
> the tag color. Valid colors are green, yellow, orange, and red, and  
> these are stored as strings in the database.
> 
> I’d like to use something like:
> 
> # Customer Model
> 
> has\_many :requests, :order =\> priority, tag\_color

That will sort them alphabetically, probably not what you want.

If there is an order those colors you could do something like this:

:order =\> “priority, tag\_color = ‘red’, tag\_color = ‘orange’,  
tag\_color = ‘yellow’, tag\_color = ‘green’”

With an optional “DESC” thrown in to each of those. Basically that  
converts your order by clause into a series of booleans and sorts that  
way. Little ugly, but it will work.

-philip

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [September 7, 2008, 4:42am UTC](https://www.ruby-forum.com/t/custom-sort-possible/147646/3 "2008-09-07T04:42:39Z")

</div>

You could also put a method like this in your Request model

def Request.sql\_tag\_color\_order  
‘(CASE requests.tag\_color WHEN ‘red’ THEN 1 WHEN ‘yellow’ THEN 2  
WHEN ‘green’ THEN 3 ELSE 4 END)’  
end

Then change the line in your Customer model to  
has\_many :requests, :order =\> “priority,  
#{Request.sql\_tag\_color\_order}”

That way when you want to order requests in another part of your  
application you can use Request.sql\_tag\_color\_order instead of having  
to repeat the transformation from text to something order.

HTH
