Sort file in dir


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Sort file in dir

  1. #1
    Join Date
    May 2004
    Posts
    7

    Question Sort file in dir

    Hi,

    I am reading a dir with files which look something like..

    ABC~1086033045490.xml
    ANOTHER~1086033045509.xml
    ABC~1086032932768.xml
    MORE~1086032932847.xml

    I would like to handle (read, do something to) each file but I need to do this in order of the after the ~. So I want to sort the files on the number that appears in the filename.

    Any ideas..?

    Thanks
    AcidHawk
    Of all the things I've lost in my life, its my mind I miss the most.

  2. #2
    Join Date
    May 2004
    Posts
    7

    Idea

    I've come up with the following..

    public String FirstFile(String dirname) {
    String filename;
    File dir = new File(dirname);

    String[] children = dir.list();
    Arrays.sort(children);
    if (children == null) {
    // Either dir does not exist or is not a directory
    filename = null;
    } else {
    filename = children[0];
    }
    return filename;
    }

    Any comments..?
    Of all the things I've lost in my life, its my mind I miss the most.

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    i think the problem is that is that the sort() method sorts the elements based on their compareTo() methods. The compareTo() of String won't work in the way that you want. It won't ignore whatever comes before the ~

    If the names of the files before the ~ are not all identicle then it won't sort them by the number. You'll have to extract the number from the string using something like the split() method of string.

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    i think the problem is that is that the sort() method sorts the elements based on their compareTo() methods. The compareTo() of String won't work in the way that you want. It won't ignore whatever comes before the ~

    If the names of the files before the ~ are not all identicle then it won't sort them by the number. You'll have to extract the number from the string using something like the split() method of string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center