List<PsiElement> (final List<? extends Pair<? extends PsiNamedElement, Integer>> elementsWithWeights, final boolean weightsAscending, @Nullable final Comparator<? super PsiElement> sameNameComparator) { elementsWithWeights.sort((o1, o2) -> { int result = weightsAscending ? o1.second.compareTo(o2.second) : o2.second.compareTo(o1.second); if (result == 0) result = Comparing.compare(o1.first.getName(), o2.first.getName()); if (result == 0 && sameNameComparator != null) result = sameNameComparator.compare(o1.first, o2.first); return result; }); final List<PsiElement> result = new ArrayList<>(elementsWithWeights.size()); for (Pair<? extends PsiNamedElement, Integer> each : elementsWithWeights) { result.add(each.first); } return result; }