• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting filenames  RSS feed

 
Sean Riley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a dir with the following filenames which I need to sort by the LAST number (the time before the file extension)

ABC~1086676244679.xml
ABC~1086676244761.xml
Another~File~1086676244701.xml
Yet~345~morefile~1086676244697.xml

I need to process these files in order( ie sort them by the 10866... number)

I was thinking about putting everything before the number into a Map key and everything after the LAST ~ but before the .xml into the respective keys corresponding values and then sorting on that.

Basically I want the files back in the following order :-

ABC~1086676244679.xml
Yet~345~morefile~1086676244697.xml
Another~File~1086676244701.xml
ABC~1086676244761.xml

How can I split the filenames and still keep the filename pieces so as to be able to refer to these files later..?

Many Thanks
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider simply creating a Comparator that knows about your particular file format.You can use this comparator to build a TreeSet or to call Collections.sort(List, Comparator). The disadvantage of this approach is that you're parsing the filenames more often than strictly necessary - O(n log n) rather than O(n) - but in most cases that shouldn't be a problem.

- Peter
[ June 08, 2004: Message edited by: Peter den Haan ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!