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

R

Robert Marley

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
 
L

Lex Williams

Robert said:
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
 
J

Jesús Gabriel y Galán

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top