• Post Reply Bookmark Topic Watch Topic
  • New Topic

sort an array  RSS feed

 
Hugo de la Mora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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!
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • 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
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!