469,282 Members | 1,704 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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
Oct 4 '05 #1
3 14370
Den Tue, 04 Oct 2005 09:12:58 -0400. skrev andrew:
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).


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.

Oct 4 '05 #2
Thanks for the reply. I found a solution by using Date, and
SimpleDateFormat class to do the sorting of the date fields:

String strDateFormat = "dd-MMM-yyyy";
SimpleDateFormat dfDate = new SimpleDateFormat(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" <th****@localhost.localdomain> wrote in message
news:pa****************************@localhost.loca ldomain...
Den Tue, 04 Oct 2005 09:12:58 -0400. skrev andrew:
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).
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.

Oct 4 '05 #3
In article <Ob******************@news20.bellglobal.com>, an****@abc.com
writes...
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 ).


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
Oct 6 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by 00steve | last post: by
7 posts views Thread by Nova's Taylor | last post: by
10 posts views Thread by Colin Steadman | last post: by
2 posts views Thread by Tim Lang | last post: by
21 posts views Thread by yeti349 | last post: by
reply views Thread by pbd22 | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.