# Sort date array without using ruby's built-in date class

**URL:** <https://www.ruby-forum.com/t/sort-date-array-without-using-rubys-built-in-date-class/148010>\
**Category:** Ruby\
**Created:** [September 8, 2008, 9:42am UTC](https://www.ruby-forum.com/t/sort-date-array-without-using-rubys-built-in-date-class/148010 "2008-09-08T09:42:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [September 8, 2008, 9:42am UTC](https://www.ruby-forum.com/t/sort-date-array-without-using-rubys-built-in-date-class/148010/1 "2008-09-08T09:42:17Z")

</div>

Say for instance I have an array of dates in string format

dates = [‘06/12/08’, ‘11/11/06’, ‘01/01/07’]

I want to sort this array in ascending order without using Ruby’s  
built-in date class. Is there a way of doing this?

Just started Ruby and I’m pretty much a newbie. I tried the sort!, but  
it sorts only by day.

Thanks  
Rob

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [September 8, 2008, 9:54am UTC](https://www.ruby-forum.com/t/sort-date-array-without-using-rubys-built-in-date-class/148010/2 "2008-09-08T09:54:28Z")

</div>

Robert M. wrote:

> Say for instance I have an array of dates in string format
> 
> dates = [‘06/12/08’, ‘11/11/06’, ‘01/01/07’]
> 
> I want to sort this array in ascending order without using Ruby’s  
> built-in date class. Is there a way of doing this?
> 
> Just started Ruby and I’m pretty much a newbie. I tried the sort!, but  
> it sorts only by day.
> 
> Thanks  
> Rob

you should split each string on / and then make the comparisons , in  
this order : year , month , day

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [September 8, 2008, 10:32am UTC](https://www.ruby-forum.com/t/sort-date-array-without-using-rubys-built-in-date-class/148010/3 "2008-09-08T10:32:32Z")

</div>

On Mon, Sep 8, 2008 at 9:37 AM, Robert M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Say for instance I have an array of dates in string format
> 
> dates = [‘06/12/08’, ‘11/11/06’, ‘01/01/07’]
> 
> I want to sort this array in ascending order without using Ruby’s  
> built-in date class. Is there a way of doing this?
> 
> Just started Ruby and I’m pretty much a newbie. I tried the sort!, but  
> it sorts only by day.

Is there any reason you don’t want to use the Date class?  
If you want to compare dates alfanumerically you need to have  
the year first, then the month and then the day. You could change your  
data to be that way and then simple string sorting will work.

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [September 8, 2008, 10:53am UTC](https://www.ruby-forum.com/t/sort-date-array-without-using-rubys-built-in-date-class/148010/4 "2008-09-08T10:53:15Z")

</div>

It’s for an assignment where we have to manipulate some dates without  
using the in-built date class. I know it’s really stupid… -\_- Anyways,  
yeah thanks for your answers guys. I’ll manage with the year/month/day  
and work from that.

Cheers  
Rob
