Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to sort files in a given directory

Posted on 2003-02-25
11
Medium Priority
?
222 Views
Last Modified: 2010-03-31
I have to sort a list of files in a given directory.

Example : There is directory with files as page1.htm,page2.htm,page20.htm,page21.htm,page12.htm, page3.htm,page4.htm,page5.htm.....

you can see the above files how are they listed, i want them to in a order of ascending as page1.htm,page2.htm,page3.htm,page4.htm,page5.htm,page10.htm,page11.htm,page20.htm,page21.htm,page22.htm

from a directory i am applying a filter and getting only .htm files from there i need to sort in ascending order. By default it is getting me some different order.

Please can anyone help out with the code.
Thank You,
Vijay



0
Comment
Question by:vijay_trina
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8018122
This should give you the basic idea:

  String[] files = { "page5.htm", "page10.htm", "page11.htm" };

  Arrays.sort(files, new Comparator() {

    public int compare(Object o1, Object o2) {
      String name1 = (String)o1;
      String name2 = (String)o2;
      String num1 = name1.substring(4, name1.indexOf("."));
      String num2 = name2.substring(4, name2.indexOf("."));
      return new Integer(num1).compareTo(new Integer(num2));
    }

  });

  for(int i = 0;i < files.length;i++) {
    System.out.println(files[i]);

  }
0
 

Expert Comment

by:GumB
ID: 8018174
You shouldn't need a Comparator though, as the default 'natural' ordering used by the Arrays class for a File object will be it's 'String' name.

So...

    String dirName = System.getProperty("user.dir");
    File dir = new File(dirName);
    File[] dirContents;
    if (dir.exists() && dir.isDirectory()) {
      dirContents = dir.listFiles();
      java.util.Arrays.sort(dirContents);
      for (int i=0; i<dirContents.length; i++) {
        System.out.println(dirContents[i]);
      }
    } else {
      System.out.println( "not a directory!" );
    }

Although knowing about Comparators will help you as well, so good job CEHJ !
0
 
LVL 35

Expert Comment

by:girionis
ID: 8018228
 This

> return new Integer(num1).compareTo(new Integer(num2));

  could also be:

return num1 - num2;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 8018252
>>Although knowing about Comparators will help you as well, so good job CEHJ !

Essential, else  "page2.htm" > "page11.htm"
0
 
LVL 35

Expert Comment

by:girionis
ID: 8018255
 Sorry ignore my post.. I thought num1 and num2 were of type int :-)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 220 total points
ID: 8018278
>>could also be:

return num1 - num2;
>>

Well it could also be

Integer.parseInt(num1) - Integer.parseInt(num2)

yes. This is cheaper and therefore better than what i wrote actually.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8018307
>>Sorry ignore my post.. I thought num1 and num2 were of type int :-)

Don't worry - the comment still lead to better code ;-)
0
 

Expert Comment

by:GumB
ID: 8018347
>> Essential, else  "page2.htm" > "page11.htm"

Ah yes, I guess my reactor just melted down and killed a million people.  Oops.

I didn't check it against numbered files ...thanks for the heads up CEHJ. At least vijay_trina learned a little more about the Arrays class today (as did I, jikes!)
0
 

Expert Comment

by:CleanupPing
ID: 9059088
vijay_trina:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9671763
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question