# Sorting an array object

**URL:** <https://www.ruby-forum.com/t/sorting-an-array-object/146223>\
**Category:** Rails\
**Created:** [August 19, 2008, 7:33am UTC](https://www.ruby-forum.com/t/sorting-an-array-object/146223 "2008-08-19T07:33:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 19, 2008, 7:33am UTC](https://www.ruby-forum.com/t/sorting-an-array-object/146223/1 "2008-08-19T07:33:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 19, 2008, 7:42am UTC](https://www.ruby-forum.com/t/sorting-an-array-object/146223/2 "2008-08-19T07:42:16Z")

</div>

> 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](http://www.google.com/search?q=javascript+table+sort)

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 19, 2008, 8:34am UTC](https://www.ruby-forum.com/t/sorting-an-array-object/146223/3 "2008-08-19T08:34:59Z")

</div>

Philip H. 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…
> 
> [javascript table sort - Google Search](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?

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 19, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/sorting-an-array-object/146223/4 "2008-08-19T14:23:43Z")

</div>

> 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.
