Reverse String?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 11
Reputation: Jezarel is an unknown quantity at this point 
Solved Threads: 0
Jezarel Jezarel is offline Offline
Newbie Poster

Reverse String?

 
0
  #1
Dec 6th, 2007
Hey guys, back again I be. Having a new problem with my dates. I want to sort by date and that's fine but in order to sort correctly by date i would have to reverse the date and ignore the slashes or it wont recognise that 2006 is before 2007 for instance as it only reads the first bit. Is there something i can do to reverse this string for sorting.

Regards
Jez
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Reverse String?

 
0
  #2
Dec 6th, 2007
Well, sort date objects rather than date strings and you wn't have this problem. Use SimpleDateFormat to create actual date objects from the date strings, then sort those.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Reverse String?

 
0
  #3
Dec 6th, 2007
Parsing them into valid dates as masijade mentioned is really best, but if you have simple date strings like "12/01/2006", you could use the String split() function to put them in year-month-day form "20031201" for integer sorting
  1. String date = "12/10/2006";
  2. String[] datePieces = date.split("/");
  3. String rebuiltDate = datePieces[2]+datePieces[0]+datePieces[1];
  4. System.out.println(rebuiltDate);
If you have times in the string as well then it gets trickier and you would need to use regex parsing to create the sortable representation - at which point you would have just been better off using the date formatter in the first place...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2595 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC