I'm reading the filenames/directories of all the files within a folder into an array of Files.
The filesnames are all of the type:

Name1, Name2, Name3, Name4, Name5, Name6, Name7, Name8, Name9, Name10, Name11, Name12, Name13

The above shows the correct order in which the files should be ordered.
However, what java does is the following:

Name1, Name10, Name11, Name12, Name13, Name2, Name3, Name4, Name5, Name6, Name7, Name8, Name9

Here is the code I used:

JFileChooser folderChooser = new JFileChooser();
        folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = folderChooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File directory = folderChooser.getSelectedFile();
            File files[] = directory.listFiles();
            Arrays.sort(files);
            }
        for (int i = 0; i < files.length; i++){
            String test = files[i].getName();
            System.out.println(test);
        }

Any ideas for an easy fix?

It is in correct alphabetical order. It's using the numerical value of each of those characters, regardless if it is a letter or number, then comparing the numerical values to do the sorting. 1 comes before 2, and it only breaks on ties, so 10 also comes before 2.

I'm way rusty on Java right now but I'd just find a library to do this for me. If not, just create a new class that implements Comparable. An easy way to do this, if your filenames are always in the format NameNumber is to use a Regular Expression to split the filename into two separate variables, name and number. Then in your compareTo method (required to implement Comparable) you'd compare the Strings. If there is a tie, then compare the numbers. For the String comparison and number comparison, you can just use Java's existing methods to do those comparisons. So implementing this shouldn't be that hard, though I'm not sure it is the best solution.

Edited 5 Years Ago by BestJewSinceJC: n/a

I know it technically is the "correct" alphabetical order, but really now, who wants their files in that counterintuitive order?
If you use alphabetic sorting of files in windows explorer for instance, it does work the way I want it to.

Agreed - that order would be preferable. Like I said, make your own class. In the class constructor, pass in your File Object. Get the String filename from that file Object. Use a Regular Expression (or even a simple character check - String.charAt(index).isDigit()) to figure out where the A-Z part of the filename ends and where the numerical part begins. Put the A-Z part in one String variable and the numerical part in an Integer variable. Make sure your class implements Comparable, and in your compareTo method, use your String variable and your Integer variable to do the comparison. For the comparison itself, you can use String.compareTo and Integer.compareTo.. when you do the String.compareTo, if there is a tie, you can resolve it by doing the Integer.compareTo..

This article has been dead for over six months. Start a new discussion instead.