• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting files on Dates  RSS feed

 
Raj Jindal
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Vector of hastables containing the name of the file and its creation Date.
e.g:- [{Date=Aug 21, 2003, Name=webservess.s}, {Date=Sep 25, 2003, Name=readFromURL.java}, {Date=Jul 26, 2003, Name=MoveSupplier.jsp}, {Date=Jan 14, 2004, Name=exportone.xml}, {Date=Jan 14, 2004, Name=export
two.xml}, {Date=Jan 3, 2003, Name=CONFIG.SYS}]
I want to print the file names in the order with the most recent date first and so on.
Can anybody please give me the idea or the code to do this. Its a bit uirgent. Thanks in advance.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Collections.sort(List list, Comparator c)
(A Vector is a List)
Brian
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would find it easier to sort the date stamp that is held as a long primitive value - milliseconds since modification - than to sort the dates as strings. See java.io.File - the lastModified method. As Brian suggests, you would then just need to write a Comparator.
Bill
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use a java.util.Date, which implements Comparable. You can parse a String as a Date using the SimpleDateFormat class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!