my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How can I sort an array of strings, but not by 1st character? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I sort an array of strings, but not by 1st character?" Watch "How can I sort an array of strings, but not by 1st character?" New topic
Author

How can I sort an array of strings, but not by 1st character?

Larry Sigmon
Greenhorn

Joined: Sep 25, 2001
Posts: 1
Each string is a date in MMDDYYYY format.
I need to sort by YYYY then MM then DD.
Arrays.sort(string)will not work.
Any ideas?
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
why dont you turn the string around so it is
yyyymmdd ?
Stephen Peterson
Ranch Hand

Joined: Dec 25, 2000
Posts: 33
Hi Larry,
I haven't done much myself yet with the Array class, but a quick look at the javadoc index (S for sort) shows
static void (sort Object[] a, Comparator c)
You can define a dateCompare class that implements the Comparator interface type that has the behavior you want in the compare method that has formal parameters like:
int compare(Object o1, Object o2)
the return value, exceptions thrown, and other methods that require implementation (equals method) can be found in javaDoc under the Comparator interface.
Granted, this reply is not a detailed, working example code listing that you can copy/paste into your program, but it has a few hints that might help you in the general direction you might want to go, and I or others can fill in missing details as you discover them.


United Health Group<BR>Hartford, CT, USA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I sort an array of strings, but not by 1st character?
 
Similar Threads
How to sort a String
Ordering Lists of Lists (Table) by more than one column
Bubble Sorting - String Literal
Can I sort String arrays?
sorting a 2d arraylist