# Sorting In rails

**URL:** <https://www.ruby-forum.com/t/sorting-in-rails/172551>\
**Category:** Rails\
**Created:** [July 23, 2009, 8:30am UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551 "2009-07-23T08:30:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 23, 2009, 8:30am UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/1 "2009-07-23T08:30:11Z")

</div>

I am executing an sql query … as like …  
abcarray = Abc.find(:all, :conditions =\> { :id =\> ‘whatever’, :name =\>  
“Hemant” } )

abcarray records are in this format:-  
id: 1, created\_at: nil, updated\_at: nil, day: 1, clinic\_id: 13, time:  
530

abcarray consists of two fields day and time actually … so i want to  
sort them using date first and then time second …

Is dere any way to do it … ?  
Waiting for u guyz …

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 23, 2009, 9:48am UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/2 "2009-07-23T09:48:23Z")

</div>

Hemant B. wrote:

> I am executing an sql query … as like …  
> abcarray = Abc.find(:all, :conditions =\> { :id =\> ‘whatever’, :name =\>  
> “Hemant” },

:order =\> ‘date, time’  
)

> abcarray records are in this format:-  
> id: 1, created\_at: nil, updated\_at: nil, day: 1, clinic\_id: 13, time:  
> 530
> 
> abcarray consists of two fields day and time actually … so i want to  
> sort them using date first and then time second …
> 
> Is dere any way to do it … ?

Now the lecture. Using ActiveRecord requires knowing SQL, even if AR  
does a very  
good job of hiding its verbosity. At the simplest extreme, AR find()  
often  
allows you to write the same kind of command, such as :group or :order,  
in the  
same way as the matching SQL GROUP BY or ORDER BY clause.

Next, a :datetime field holds both the date and time together, so you  
ought to  
edit your model’s migration and run rake db:migrate:reset to erase all  
your data  
and put those two fields together!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 23, 2009, 10:36am UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/3 "2009-07-23T10:36:29Z")

</div>

I had done it …  
using abcarray = Abc.find(:all, :conditions =\> { :id =\> ‘whatever’,  
:name =\> “Hemant” }, :order =\> ‘day’ )  
and then …  
abcarray.sort {|a,b| a.time \<=\> b.time}

I think this is the right way to do it … ?  
What say champs …? 🙂

Phlip wrote:

> Hemant B. wrote:
> 
> > I am executing an sql query … as like …  
> > abcarray = Abc.find(:all, :conditions =\> { :id =\> ‘whatever’, :name =\>  
> > “Hemant” },
> 
> :order =\> ‘date, time’  
> )
> 
> > abcarray records are in this format:-  
> > id: 1, created\_at: nil, updated\_at: nil, day: 1, clinic\_id: 13, time:  
> > 530

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 23, 2009, 1:53pm UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/4 "2009-07-23T13:53:15Z")

</div>

Hemant B. wrote:

> I had done it …  
> using abcarray = Abc.find(:all, :conditions =\> { :id =\> ‘whatever’,  
> :name =\> “Hemant” }, :order =\> ‘day’ )  
> and then …  
> abcarray.sort {|a,b| a.time \<=\> b.time}
> 
> I think this is the right way to do it … ?  
> What say champs …? 🙂

Not only is it the wrong way (you should be using :order =\> ‘date,  
time’), but it’s clear from your post that you didn’t pay much attention  
to the answer Phlip already gave you.

Hemant, I’ve noticed that you post a lot of beginner questions on this  
list that could be answered by a quick look in the Rails docs, or at  
least by a good book or the Rails Guides. May I suggest that you spend  
a little less time posting to the list and a little more time reading  
documentation?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 23, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/5 "2009-07-23T16:28:55Z")

</div>

Hemant if you do not own this book, you should consider purchasing it:

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

It is a _must_ have for anyone new to Rails. The Ruby book from  
PragProg is also pretty good as a reference guide to the language as a  
whole.

On Jul 23, 6:53 am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 24, 2009, 7:38am UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/6 "2009-07-24T07:38:28Z")

</div>

Ok …  
Thanks … I have this book …

Zach Karpinski wrote:

> Hemant if you do not own this book, you should consider purchasing it:  
> [Pragmatic Bookshelf: By Developers, For Developers](http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition)
> 
> It is a _must_ have for anyone new to Rails. The Ruby book from  
> PragProg is also pretty good as a reference guide to the language as a  
> whole.
> 
> On Jul 23, 6:53ï¿½am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 24, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/7 "2009-07-24T21:57:17Z")

</div>

2009/7/24 Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The above method:-
> 
> :order =\> ‘day, time\_start, time\_end’ is returning me records only in  
> day sorted… This line is not sorting in context of time\_start and  
> time\_end …
> 
> 😢

Have a look in the rails log (in your application/log) and see what  
sql query is being used, this may give you a clue.  
If it looks correct check what type the time columns are.

Colin

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [July 24, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/sorting-in-rails/172551/8 "2009-07-24T14:35:01Z")

</div>

The above method:-

:order =\> ‘day, time\_start, time\_end’ is returning me records only in  
day sorted… This line is not sorting in context of time\_start and  
time\_end …

😢

Hemant B. wrote:

> Ok …  
> Thanks … I have this book …
> 
> Zach Karpinski wrote:
> 
> > Hemant if you do not own this book, you should consider purchasing it:  
> > [Pragmatic Bookshelf: By Developers, For Developers](http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition)
> > 
> > It is a _must_ have for anyone new to Rails. The Ruby book from  
> > PragProg is also pretty good as a reference guide to the language as a  
> > whole.
> > 
> > On Jul 23, 6:53ï¿½am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
