?
Solved

How to sort files in a given directory

Posted on 2003-02-25
11
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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