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

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

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

On Mon, Sep 8, 2008 at 9:37 AM, Robert M. [email protected] 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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs