List<String> (@NotNull Collection<String> keys, @NotNull String pattern, ProgressIndicator progressIndicator) { Int2ObjectMap<List<String>> priority = new Int2ObjectOpenHashMap<>(); for (String key : keys) { progressIndicator.checkCanceled(); int start = key.indexOf(pattern); if (start == -1) { continue; } if (start > 0 && key.charAt(start - 1) != '.') { continue; } if (start + pattern.length() < key.length() && key.indexOf(".", start + pattern.length()) != -1) { continue; } int dots = countDots(key, start); priority.computeIfAbsent(dots, __ -> new SmartList<>()).add(key); } progressIndicator.checkCanceled(); IntList listToSort = new IntArrayList(priority.keySet()); listToSort.sort(null); List<String> result = new ArrayList<>(); for (IntListIterator iterator = listToSort.iterator(); iterator.hasNext(); ) { int index = iterator.nextInt(); List<String> found = priority.get(index); List<String> toSort = new ArrayList<>(found); toSort.sort(null); result.addAll(toSort); } return result; }