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

Discussion in 'Ruby' started by Robert Marley, Sep 8, 2008.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Robert Marley, Sep 8, 2008
    #1
    1. Advertising

  2. Robert Marley

    Lex Williams Guest

    Robert Marley 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Lex Williams, Sep 8, 2008
    #2
    1. Advertising

  3. On Mon, Sep 8, 2008 at 9:37 AM, Robert Marley <> 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.
     
    Jesús Gabriel y Galán, Sep 8, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rkk
    Replies:
    9
    Views:
    840
    CBFalconer
    Sep 24, 2006
  2. Navin
    Replies:
    1
    Views:
    742
    Ken Schaefer
    Sep 9, 2003
  3. Replies:
    9
    Views:
    428
    whisperjim
    Nov 27, 2008
  4. Replies:
    0
    Views:
    268
  5. Domenico Discepola

    multi-field array sort using Sort::Fields method

    Domenico Discepola, Apr 27, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    308
    Uri Guttman
    Apr 28, 2004
Loading...

Share This Page