# Order by returning strange results

**URL:** <https://www.ruby-forum.com/t/order-by-returning-strange-results/104135>\
**Category:** Rails\
**Created:** [May 14, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/order-by-returning-strange-results/104135 "2007-05-14T17:36:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [May 14, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/order-by-returning-strange-results/104135/1 "2007-05-14T17:36:40Z")

</div>

Hi,

I am trying to make a call to find, and have the result set ordered.  
However, I am experiencing some strange behaviour. The column I want  
to sort on is of type varchar, but actually contains floating point  
numbers. (I needed to switch the type to varchar to get validation on  
the input working properly). So when I just try to order, it returns  
in what seems to be a properly ordered (in terms of strings)  
collection. However, I am trying to get it to return the collection  
sorted in terms of float values. I tried just returning the  
collection and then using sort\_by with column.to\_f, but this wasn’t  
working (even though when I tried it just through IRB that call to  
sort\_by on an array of strings worked fine). Here is how the “sorted”  
list came out using this method:

45.418185  
45.4226533  
45.11111  
45.333333  
-75.123213  
-12.12344  
-12.12344  
13  
-99

So then I tried to implement it in the SQL query, using CAST (ie,  
ORDER BY CAST(column as SIGNED) ) but this is returning some strange  
results too. For example, here is the “sorted” output of using CAST:

-99  
-75.123213  
-12.12344  
-12.12344  
13  
45.333333  
45.11111  
45.4226533  
45.418185

Does anybody have any ideas whats going on, or any other approaches  
that I could try to get this working?

Thanks,

Simon
