Java sort String array of file names by their extension

I have an array of file names and need to sort this array by file name extensions. Is there an easy way to do this?

+5
source share
8 answers
Arrays.sort(filenames, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        // the +1 is to avoid including the '.' in the extension and to avoid exceptions
        // EDIT:
        // We first need to make sure that either both files or neither file
        // has an extension (otherwise we'll end up comparing the extension of one
        // to the start of the other, or else throwing an exception)
        final int s1Dot = s1.lastIndexOf('.');
        final int s2Dot = s2.lastIndexOf('.');
        if ((s1Dot == -1) == (s2Dot == -1)) { // both or neither
            s1 = s1.substring(s1Dot + 1);
            s2 = s2.substring(s2Dot + 1);
            return s1.compareTo(s2);
        } else if (s1Dot == -1) { // only s2 has an extension, so s1 goes first
            return -1;
        } else { // only s1 has an extension, so s1 goes second
            return 1;
        }
    }
});

For completeness: java.util.Arraysand java.util.Comparator.

+20
source

If I remember correctly, Arrays.sort (...) takes a Comparator <>, which it will use for sorting. You can provide an implementation that looks at part of a string extension.

+4
source

Comparator . '.'.

Arrays.sort(stringArray, yourComparator);

//  An implementation of the compare method
public int compare(String o1, String o2) {
    return o1.substring(o1.lastIndexOf('.')).compareTo(o2.substring(o2.lastIndexOf('.'));
}
+3

, , O (n log n). - (, ) , , TreeMap , .

import java.util.Arrays;
import java.util.TreeMap;

public class Bar {

    public static void main(String[] args) {
        TreeMap<String, String> m2 = new TreeMap<String, String>();
        for (String string : Arrays.asList(new String[] { "#3", "#2", "#1" })) {
            String key = string.substring(string.length() - 1);
            String value = string;
            m2.put(key, value);
        }
        System.out.println(m2.values());
    }
}

[#1, #2, #3]

.

, O (n) - ( - O (n log n)). n , .

+2
+1

Comparator, . Arrays.sort Comparator.

+1
    String DELIMETER = File.separator + ".";
    List<String> orginalList = new CopyOnWriteArrayList<>(Arrays.asList(listOfFileNames));
    Set<String> setOfuniqueExtension = new TreeSet<>();

    for (String item : listOfFileNames) {
        if (item.contains(".")) {
            String[] split = item.split(DELIMETER);
            String temp = "." + split[split.length - 1];
            setOfuniqueExtension.add(temp);
        }
    }

    List<String> finalListOfAllFiles = new LinkedList<>();
    setOfuniqueExtension.stream().forEach((s1) -> {
        for (int i = 0; i < orginalList.size(); i++) {
            if (orginalList.get(i).contains(s1)) {
                finalListOfAllFiles.add(orginalList.get(i));
                orginalList.remove(orginalList.get(i));
                i--;
            }
        }
    });

    orginalList.stream().filter((s1) -> (!finalListOfAllFiles.contains(s1))).forEach((s1) -> {
        finalListOfAllFiles.add(s1);
    });

    return finalListOfAllFiles;
+1

, :

, , , , ".". .

Arrays.sort(ary, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        String r1 = new StringBuffer(o1).reverse().toString();
        String r2 = new StringBuffer(o2).reverse().toString();
        return r1.compareTo(r2);
    }
});

, java reverse().

-1

All Articles