# Sorting the array

**URL:** <https://www.ruby-forum.com/t/sorting-the-array/90147>\
**Category:** Rails\
**Created:** [March 26, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/sorting-the-array/90147 "2007-03-26T13:25:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [March 26, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/sorting-the-array/90147/1 "2007-03-26T13:25:55Z")

</div>

Hai

```
I am facing the problem while try to sort. For example i have one

```

object which is the result of query. That contains two field message and  
updated\_at.Same way i ahve another object with the same field.

Now i have merged the two object. After merging i am getting like  
this,

@attributes={“message”=\>“Hai this is first good message”,  
“updated\_at”=\>“2007-03-26 11:14:02”}\>, @attributes={“message”=\>"",  
“updated\_at”=\>“2007-03-26 14:14:51”, }\>,

Now I want to sort the result according to the updated\_at field?

Can any one help me?

Regards  
Selvaraj

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [March 26, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/sorting-the-array/90147/2 "2007-03-26T14:10:45Z")

</div>

Use:  
@attributes.sort\_by{|a|a.updated\_at}

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [March 26, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/sorting-the-array/90147/3 "2007-03-26T14:42:50Z")

</div>

selvaraj wrote:

> “updated\_at”=\>“2007-03-26 11:14:02”}\>, @attributes={“message”=\>"",  
> “updated\_at”=\>“2007-03-26 14:14:51”, }\>,
> 
> Now I want to sort the result according to the updated\_at field?
> 
> Can any one help me?
> 
> Regards  
> Selvaraj

If you’ve merged things into an array you can do something like:

sorted\_array = myarray.sort { |a,b| a[“updated\_at”] \<=\> b[“updated\_at”]  
}

If you want the earliest hash at the beginning of the array. Otherwise  
you can swap a and b around in the comparison.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [March 26, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/sorting-the-array/90147/4 "2007-03-26T16:21:11Z")

</div>

Thank u very much,

Its working fine.

Michael W. wrote:

> selvaraj wrote:
> 
> > “updated\_at”=\>“2007-03-26 11:14:02”}\>, @attributes={“message”=\>"",  
> > “updated\_at”=\>“2007-03-26 14:14:51”, }\>,
> > 
> > Now I want to sort the result according to the updated\_at field?
> > 
> > Can any one help me?
> > 
> > Regards  
> > Selvaraj
> 
> If you’ve merged things into an array you can do something like:
> 
> sorted\_array = myarray.sort { |a,b| a[“updated\_at”] \<=\> b[“updated\_at”]  
> }
> 
> If you want the earliest hash at the beginning of the array. Otherwise  
> you can swap a and b around in the comparison.
> 
> –  
> Michael W.
