• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting filenames

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic