Ruby Forum Ruby on Rails > Sorting an array object

Posted by Tushar Gandhi (tushar_gandhi)
on 19.08.2008 07:33
Hi,
    I have three mechanisms for sorting 1) First Name 2) Last Name 3)
Created Date
Currently, I am doing query for each type of sorting. That means I am
hitting database 3 times. I want to do like this  it will hit database
at only first time and after that I will sort that array of object
depending on the sorting user selected, on view itself. Is it possible?
If yes please tell me.
   I am tried this but I am not able to do that. After that I done like
this, Whenever user select sorting then I am giving an Ajax call. Going
to controller with that array of objects and then tried to sort that
array by using array sort method. But problem is that I am getting that
array as a string from URL and the scope of object is lost. Anyone
please tell me how to solve this problem?

Thanks & Regards,
Tushar
Posted by Philip Hallstrom (Guest)
on 19.08.2008 07:42
(Received via mailing list)
> this, Whenever user select sorting then I am giving an Ajax call.  
> Going
> to controller with that array of objects and then tried to sort that
> array by using array sort method. But problem is that I am getting  
> that
> array as a string from URL and the scope of object is lost. Anyone
> please tell me how to solve this problem?

You want client side sorting via javascript.  See below...

http://www.google.com/search?q=javascript+table+sort
Posted by Tushar Gandhi (tushar_gandhi)
on 19.08.2008 08:34
Philip Hallstrom wrote:
>> this, Whenever user select sorting then I am giving an Ajax call.  
>> Going
>> to controller with that array of objects and then tried to sort that
>> array by using array sort method. But problem is that I am getting  
>> that
>> array as a string from URL and the scope of object is lost. Anyone
>> please tell me how to solve this problem?
> 
> You want client side sorting via javascript.  See below...
> 
> http://www.google.com/search?q=javascript+table+sort

Hi,
   I don't want a client side sorting at all. Can we not able to do in 
ruby itself?
Posted by Ar Chron (railsdog)
on 19.08.2008 14:23
>    I don't want a client side sorting at all. Can we not able to do in 
> ruby itself?

If what you're looking for is server-side sorting, why not let the 
database do it?
Create an index based on each of those sort options (first name, last 
name, created date). Unless your database server is poo poo, that should 
be plenty fast, and certainly easier than your current worries.

If you consider the mechanics of what you're trying to do, you can't 
just ship objects to and fro on the web, at least not without some 
additional work that usually consists of reconstituting your objects 
once they come back from the browser.

You could look into implementing object caching in some fashion on the 
server side. Rails 2.0.2, if I recall correctly, provides a greatly 
expanded caching mechanism, but you are adding additional maintenance 
work there as well.

Personally I like the "keep it simple" principle, followed closely by 
"make it work first, optimize later".  Perhaps your database access is 
not as onerous as you think.