Java tutorial
import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { String[] testArray = { "/", "/games/", "/homework/", "/usr/", "/games/snake/", "/temp/downloads/", "/usr/local/", "/usr/local/bin/" }; Comparator<String> pathComparator = new PathComparator(); Arrays.sort(testArray, pathComparator); System.out.println(Arrays.toString(testArray)); } } class PathComparator implements Comparator<String> { public int compare(String path1, String path2) { String[] dirs1 = path1.split("/"); String[] dirs2 = path2.split("/"); if (dirs1.length < dirs2.length) { return -1; } else if (dirs1.length > dirs2.length) { return 1; } else { String lastDir1 = dirs1[dirs1.length - 1]; String lastDir2 = dirs2[dirs2.length - 1]; return lastDir1.compareTo(lastDir2); } } }