Alex Rivera | Logout

Java how to sort lowercase before uppercase strings

Asked 2013-05-07T16:45:54.100
12

I want the files to be ordered by their abs path name, but I want the lowercase to be sorted before the uppercase. Example: Let's say I got 4 files:

files2.add("b");
files2.add("A");
files2.add("a");
files2.add("B");

the order with this code is: [A, B, a, b] I want it to be: [a, A, b, B]

import java.io.File;
import java.util.*;

public class Abs {

    public ArrayList<File> getOrder(ArrayList<File> files) {
        Collections.sort(files, new Comparator<File>() {
            public int compare(File file1, File file2) {
                return file1.getAbsolutePath().compareTo(file2.getAbsolutePath());
            }
        });
        return files;
    }

}
Edit
Report

1 Answer

1

You could implement your own Comparator, which in turn uses a Collator. See example.

answered 2013-05-07T16:54:11.453

Your Answer