# Sorting object by date

**URL:** <https://www.ruby-forum.com/t/sorting-object-by-date/84711>\
**Category:** Ruby\
**Created:** [February 7, 2007, 4:54am UTC](https://www.ruby-forum.com/t/sorting-object-by-date/84711 "2007-02-07T04:54:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 4:54am UTC](https://www.ruby-forum.com/t/sorting-object-by-date/84711/1 "2007-02-07T04:54:27Z")

</div>

Hey guys,  
I need a fast way to sort an array of object by their .time in xml time  
scheme. In java,it would be easy using priority queue,is there any thing  
similar to priority queue in ruby? Since this is a very small app, it  
would be waste of time trying to implement a priority queue.  
Thai

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/sorting-object-by-date/84711/2 "2007-02-07T23:33:56Z")

</div>

On 2/7/07, Thai Le [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey guys,  
> I need a fast way to sort an array of object by their .time in xml time  
> scheme. In java,it would be easy using priority queue,is there any thing  
> similar to priority queue in ruby? Since this is a very small app, it  
> would be waste of time trying to implement a priority queue.  
> Thai

In ruby, you can sort array either by using Array#sort, or  
Array#sort\_by.  
Provided that your .time values have proper \<=\> operator, you can do:

sorted\_array = array\_of\_objects.sort\_by {|o| o.time}

If the \<=\> operator is not available, you can replace o.time above  
with e.g. conversion to timestamp, or implement your own:

sorted\_array = array\_of\_objects.sort {|a,b| …put here the  
comparison…}

(the sort by version is faster, as it makes the conversion only once  
per array item.

For more details see documentation  
([class Array - RDoc Documentation](http://ruby-doc.org/core/classes/Array.html#M002235))
