# Weird sort

**URL:** <https://www.ruby-forum.com/t/weird-sort/184458>\
**Category:** Rails\
**Created:** [March 2, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/weird-sort/184458 "2010-03-02T21:51:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kampers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kampers](https://www.ruby-forum.com/u/kampers)\
**Post date:** [March 2, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/weird-sort/184458/1 "2010-03-02T21:51:00Z")

</div>

Hi guys: Having this strange problem using Rails on Postgres. I’m trying  
to count up the records that match a certain created\_at date, but they  
don’t seem to be grouping in order. Tip: this worked fine in MySQL, but  
I need to use Heroku, which is Postgres only…

Here is my query:

@graphdata = Capture.count(:group =\>  
“to\_char(created\_at,‘YYYYMMDD000000’)”, :conditions =\> “created\_at  
between '”+@rangestart+"’ and ‘"+@rangeend+"’ and ("+@searchterms+")",  
:select =\> “created\_at”)

But when I try to output this, the order is all wonky:

20100302000000:994\<  
20100222000000:2964\<  
20100227000000:4853\<  
20100301000000:7143\<  
20100226000000:5340\<  
20100223000000:3517\<  
20100221000000:196\<  
20100225000000:4485\<  
20100224000000:4068\<  
20100228000000:15335\<

Any ideas? I tried adding :order to the query and it broke…

Thanks…Chris

---

<div class="post-metadata">

**Author:** ![kampers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/54ee81/32.png) [@kampers](https://www.ruby-forum.com/u/kampers)\
**Post date:** [March 3, 2010, 3:04am UTC](https://www.ruby-forum.com/t/weird-sort/184458/2 "2010-03-03T03:04:14Z")

</div>

What was in your :order argument?

Did you specify it as :order =\> “to\_char(created\_at,  
‘YYYYMMDD00000’)” ?
