-3

I have a list of files named as file001-file002...file099. I used the following code to sort them but still my files are listed as file001-file010-file011...file002-file020... while they should be sorted alphabetically as file001 file002 file003 file004 file005 ...file099.

How to fix this?

File folder = new File("g:\");

 File[] listOfFiles = folder.listFiles();
 Arrays.sort(listOfFiles);

for (File file : listOfFiles) {
    if (file.isFile()) {
        //do sth
    }
}

EDIT

I would like my files to be ordered based on their name alphanumerically:

 file001
 file002
 file003
 file004
 file005
 ...file099
lonesome
  • 2,503
  • 6
  • 35
  • 61
  • 2
    Instead of sorting files, sort the file names. – PythonLearner Jan 02 '20 at 19:40
  • @Deb so...what is sorting file then? – lonesome Jan 02 '20 at 19:43
  • 2
    Alternatively, give a proper comparator to the sort method : `Arrays.sort(files, Comparator.comparing(File::getName));` should produce the expected result. – jrook Jan 02 '20 at 19:43
  • @jrook no it didnt. – lonesome Jan 02 '20 at 19:45
  • I'm not sure what you are expecting. Please show the expected and actual output: what you have currently is unclear. – Andy Turner Jan 02 '20 at 19:46
  • 2
    Natural ordering for `String` is lexicographic, you are looking for natural ordering where a given `String` might contain numbers and you want to sort alphabetically on the letters but numerically on the numbers. – x80486 Jan 02 '20 at 19:47
  • 2
    [Sorting the numeric filenames works fine for me](https://ideone.com/EV4sf3). Please provide a [mcve]. – Andy Turner Jan 02 '20 at 19:51
  • 1
    I just want my files to be ordered like how an OS does. – lonesome Jan 02 '20 at 19:52
  • There's no such "like an OS does". File sorting on a OS depends heavily on the OS itself and in some it even depends on the command you are using, some have a default sorting, some rely on the command used to list the files. Therefore everyone is asking what are YOU expecting out of your code. – Jorge Campos Jan 02 '20 at 20:03
  • @JorgeCampos I already mentioned it in my question. Should be ordered as `file001 file002 file003` and not `file001 file010 file011` – lonesome Jan 02 '20 at 20:07
  • 3
    Since `new File("g:\")` does not compile, I assume this is not your actual code. The files you describe should be sorted as you expect, assuming they all start with the four letters `f`, `i`, `l`, `e`, and end with exactly three digits. Edit your question and show actual code that we can test ourselves. (I suspect you do not in fact have a file named `file001`, but rather `file01`—two digits, not three.) – VGR Jan 02 '20 at 20:09

1 Answers1

0

It's Alphanum algorithm and well explained here: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ and has answered here Sort on a string that may contain a number.

The_Cute_Hedgehog
  • 1,280
  • 13
  • 22
  • 4
    Hi, please don't post answers with just links. And also don't post link of other answers. First because links can be broken and your answer is no longer useful, second if you know that SO already have an answer to it just flag the question as duplicate. If you think the link you provided is really useful, add the important parts of it in your answer, the link should be just a reference to it. Thank you. – Jorge Campos Jan 02 '20 at 20:02