Sort Date Field String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew

    #1

    Sort Date Field String

    Hi:

    I am already using TreeMap to massage records in my export file such that
    each record has a unique key combination ( LastName + FirstName + Member
    Key) . Thus I am sorting the records by the unique key. I also have a Date
    String Field in my record ( ie "30-Apr-2005" ). I want to create a unique
    key combination of
    ( LastName + FirstName + Member Key + Date).

    Can you please tell me what are the steps I need to do to sort the Date
    String Field? Obviously, I can't treat the date as a string field because
    it will sort the date alphabetically ( ie 30-Apr-2005 will come before
    30-Jan-2005 ).




    Thanks in advance

    Andrew


  • Thomas Dybdahl Ahle

    #2
    Re: Sort Date Field String

    Den Tue, 04 Oct 2005 09:12:58 -0400. skrev andrew:[color=blue]
    > I am already using TreeMap to massage records in my export file such that
    > each record has a unique key combination ( LastName + FirstName + Member
    > Key) . Thus I am sorting the records by the unique key. I also have a Date
    > String Field in my record ( ie "30-Apr-2005" ). I want to create a unique
    > key combination of
    > ( LastName + FirstName + Member Key + Date).[/color]

    What about making/using a method for turning dates into an int or long.
    Then making a SortedMap using the number as key and string as value.

    It's all about making/using a fast algorithm for the convertion.

    --
    Programmers should realize their critical importance and responsibility in a
    world gone digital. They are in many ways similar to the priests and monks of
    Europe's Dark Ages; they are the only ones with the training and insight
    to read and interpret the "scripture" of this age.

    Comment

    • andrew

      #3
      Re: Sort Date Field String

      Thanks for the reply. I found a solution by using Date, and
      SimpleDateForma t class to do the sorting of the date fields:

      String strDateFormat = "dd-MMM-yyyy";
      SimpleDateForma t dfDate = new SimpleDateForma t(strDateFormat );
      Date dateKey = null;
      dateKey = dfDate.parse( datefield ); // datefield has the
      string value "31-Jan-2005" etc.


      Then I can put dateKey as part of the key in a TreeMap so that it can be
      sorted. :)

      Thank you.


      "Thomas Dybdahl Ahle" <thomas@localho st.localdomain> wrote in message
      news:pan.2005.1 0.04.16.14.53.7 00495@localhost .localdomain...[color=blue]
      > Den Tue, 04 Oct 2005 09:12:58 -0400. skrev andrew:[color=green]
      > > I am already using TreeMap to massage records in my export file such[/color][/color]
      that[color=blue][color=green]
      > > each record has a unique key combination ( LastName + FirstName + Member
      > > Key) . Thus I am sorting the records by the unique key. I also have a[/color][/color]
      Date[color=blue][color=green]
      > > String Field in my record ( ie "30-Apr-2005" ). I want to create a[/color][/color]
      unique[color=blue][color=green]
      > > key combination of
      > > ( LastName + FirstName + Member Key + Date).[/color]
      >
      > What about making/using a method for turning dates into an int or long.
      > Then making a SortedMap using the number as key and string as value.
      >
      > It's all about making/using a fast algorithm for the convertion.
      >
      > --
      > Programmers should realize their critical importance and responsibility in[/color]
      a[color=blue]
      > world gone digital. They are in many ways similar to the priests and monks[/color]
      of[color=blue]
      > Europe's Dark Ages; they are the only ones with the training and insight
      > to read and interpret the "scripture" of this age.
      >[/color]


      Comment

      • David Moss

        #4
        Re: Sort Date Field String

        In article <Obv0f.1899$R4. 253037@news20.b ellglobal.com>, andrew@abc.com
        writes...
        [color=blue]
        > Hi:
        >
        > I am already using TreeMap to massage records in my export file such that
        > each record has a unique key combination ( LastName + FirstName + Member
        > Key) . Thus I am sorting the records by the unique key. I also have a Date
        > String Field in my record ( ie "30-Apr-2005" ). I want to create a unique
        > key combination of
        > ( LastName + FirstName + Member Key + Date).
        >
        > Can you please tell me what are the steps I need to do to sort the Date
        > String Field? Obviously, I can't treat the date as a string field because
        > it will sort the date alphabetically ( ie 30-Apr-2005 will come before
        > 30-Jan-2005 ).[/color]

        Date objects store the date as the number of milliseconds since a
        particular point in time.

        Instead of storing your dates in human readable form as a String, store
        the number instead. That way you can sort and compare very quickly.

        You can also reconstruct a Date object from the number any time you like
        and use standard Java date manipulation and comparison techniques with
        it.

        --
        DM
        personal opinion only

        Comment

        Working...