• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

sort an array

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any way to sort an array by creation date using the File class...

File file = new File("../something");
String[] fileList = file.list();

i want to get the fileList array sorted by creationdate, thx in advance
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the Arrays class, which has static methods to sort an array. You'll want a sort signature that takes a Comparator. I'll give you a chance to dig through the doc and see if you can make that work on your own ... give it a try and holler if you get stuck!
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can use the one that takes an Object[], as long as you can be sure that the actual objects being passed implement java.lang.Comparable.

You dont get the compile time error checking that you would if you follow Stan's suggestion, but this might be simpler for you.
 
Hugo de la Mora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stan and Sonny for answer, but i'm not sure if usign methods like Arrays.sort(array); will resolve this issue, what im trying to do is...
I have the following code:

File file = new File("../uploads");
String[] fList = file.list();
...
for(int n=start;n<startF;n++){
File f2 = new File("../uploads/"+fList[n]);
System.out.println("name="+fList[n]):
System.out.println("last modi="+new java.sql.Date(f2.lastModified()));
}
...so I want to sort that list by lastModified attribute, is there a short way to do that, because i kwon that the long way is put a couple of lists and then sort both, i was wondering is there are a short way...thx in advance for your time.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how you define "short" but try this ...

This was not compiled or tested We create a File object for each filename and put all the File objects into a Set. The TreeSet happens to keep its elements in sorted order. To sort it needs to compare files and it uses a Comparator that we give it to do the comparisons. I'll let you dig into Comparator and see if you can figure out how to make it work.

The for( File file : files ) syntax is valid in Java5 only. In earlier JDKs you'd get an iterator on the Set. Give it a try and show us what you get.
[ June 16, 2005: Message edited by: Stan James ]
 
Hugo de la Mora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did not understand the treeset and the comparator, i read some tutorials about it, but i did not get it, so if you could give more help that would be great, thx
 
rubbery bacon. crispy tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic