• 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

java.io.File problem

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to get list of file names in a directory, which i'm doing by following code



I have two problems
1. How can i sort the file names in ascending order?
2. How can i sort the file names by creation date (olderst file first)
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According the API specification it does not guarantee any order. Refer the javadocs for "File" from the below URL:

http://java.sun.com/j2se/1.5.0/docs/api/
 
safraz hanas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the early reply.

I have used the following code.


It gave me something like

SybTest060dat
SybTest060datxxxx
Test200000200.txt
abc.txt
abc181.txt

Again, i have a problem, how can i get File names starting "A" first & "a" after. Arrays.sort() doesn't support for these kind of scenarios.

Yours assistance is highly appreciated. eagerly waiting for a reply

Thank you in advance
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays.sort(Object[]); will sort in the ascending order, hence upper case files will be followed by lowercase files (as in your case). If you want to sort alphabetically irrespective of their case (A,a,B,b etc) then you need write your own sorting utility. You could take the first character of all the file name and proceed sorting. I am not aware of any straightforward sorting.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take this this works fien.. run this and enjoy

String temp_arr[];
File srcFolder = new File("D:\\temp\\");
srcFolder.list();
temp_arr = srcFolder.list();
Arrays.sort(temp_arr, String.CASE_INSENSITIVE_ORDER);

for(int i=0 ; i<temp_arr.length; i++)
System.out.println(temp_arr[i]);
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey, that works but the thing is that it gives array purely in alphabetical order, and gives Capital letter word first.

try this and let me know
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to sort in a different order, find the Comparator interface and create an object from that. You only have to implement the compareTo() method; you can leave the equals() method alone.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you mean the int compare(objone, objtwo) method, because a Comparator has not a compareTo method.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Safraz,

Try this code:

After compiling the code,
goto the command line and type:
> java DemoList *


Got it?


Regards,
cmbhatt
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramko Strating, you are correct; I gave the wrong name for the method.
 
safraz hanas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,

I know that i have to implement compare(Obj, Obj). However i don't have any clue how should i write it, i mean the logic. Can anyone suggest me.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to suggest anything when we don't know your requirements. I thought the answer to your question was to use String.CASE_INSENSITIVE_ORDER, too, but you say it's not. So what ordering do you want to use instead of that?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For compare(Object o1, Object o2)
  • Decide which criteria you are going to use to order the pair.
  • Set up a Comparator object using the java.util.Comparator interface.
  • Put the type required in <>, eg Comparator<Foo>
  • Instantiate your Comparator like this fooComparator = new Comparator<Foo>();
  • Put cursor before the last ; and push enter key to get new line
  • Put { [new line] } in the gap
  • enter the method body "public int compare(Foo f1, Foo f2)[new line]{ [new line] }" between the last {}
  • Put a test in for whichever criterion/criteria you have chosen, so a negative result is found if f1 < f2, zero if f1 == f2 and positive if f1 > f2.
  • Put in a statement to return that result (as an int).
  • Pass that Comparator object as the second argument to the sort method: Arrays.sort(fooArray, fooComparator);
  • The first bit, deciding how to determine that f1 < f2 or f1 > f2 is the most difficult.

    Read this part of the API and this part of the Java Tutorial.
    [edit] Few minor corrections [/edit]
    [ April 09, 2007: Message edited by: Campbell Ritchie ]
    reply
      Bookmark Topic Watch Topic
    • New Topic