-1

I have two directories as the sources for the final list of files. The files are numbered and need to be sorted in the final ArrayList of file names.

dir1/file1
dir1/file2
dir2/file3
dir1/file4

How do you sort the ArrayList based on just the file names and not the directories?

GianniTee
  • 147
  • 1
  • 9

2 Answers2

3

In order to change sorting order, you can implement you own Comparator and use it to change the default order by using Collections.sort(arrayList, c).
The following example compares lexicographically by file-name ignoring case differences:

java.util.Comparator<String> c = new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        String f1 = o1.substring(Math.max(0, o1.lastIndexOf("/")), o1.length());
        String f2 = o2.substring(Math.max(0, o2.lastIndexOf("/")), o2.length());
        return f1.compareToIgnoreCase(f2);
    }
};
Collections.sort(arrayList, c);
Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23
  • Very interesting. Maybe you can shorten the key lines to filename = filename.substring(filename.lastIndexOf('/') + 1); – GianniTee Jan 05 '16 at 16:26
  • What if we do a descending sort from the right to the left? Would that trick work? – GianniTee Jan 05 '16 at 16:27
  • What do you mean by "descending sort from the right to the left"? – Leet-Falcon Jan 06 '16 at 05:38
  • Its not a good idea - number sorting as a string value. because above code behaviour changes if file names ends with file22, file36. – Bharatesh Jan 06 '16 at 12:30
  • @bharat - True. It's stated that this is a lexicographic ordering example. – Leet-Falcon Jan 06 '16 at 12:59
  • @Leet-Falcon Thanks. sorry I didnt notice that. – Bharatesh Jan 06 '16 at 13:08
  • @Leet-Falcon Just a thought: sort by the first letter in a columnar view from the right, then by the second letter, the third and so forth. In the end, the DIRs are sorted naturally and by coincidence, the FILEs as well. Another idea I had was to sort an ArrayList with the whole strings based on the sorted ArrayList with only the FILEs. – GianniTee Jan 07 '16 at 02:18
  • The main concept here, is that by implementing your own Comparator, you could specify any order you require. If you wish for an alpha-numeric comparator, you can find one here: http://stackoverflow.com/questions/1262239/natural-sort-order-string-comparison-in-java-is-one-built-in#answers-header – Leet-Falcon Jan 07 '16 at 06:02
1

This is not really an answer, just addition to Lee-Falcon's correct answer.

Comparator should always sort in natural ordering (which is lexicographcally for strings and asceding for numbers).

Once you have implementation and you want to change the ordering, you can simply replace

return value;

with

return -1 * value;

but I'd recommend to use reverse comparator

    Integer[] ar = { 1, 3, 2, 4};
    Arrays.sort(ar); // sort with natural ordering
    System.out.println(Arrays.toString(ar));
    Arrays.sort(ar, Collections.reverseOrder()); // sort with reverse ordering
    System.out.println(Arrays.toString(ar));

Collections.reverseOrder() accepts also comparator instance:

    Arrays.sort(ar, Collections.reverseOrder(c));
Community
  • 1
  • 1
Betlista
  • 10,327
  • 13
  • 69
  • 110