Comparator<PyImportElement> () { final Comparator<String> stringComparator = AddImportHelper.getImportTextComparator(myFile); final Comparator<QualifiedName> qNamesComparator = Comparator.comparing(QualifiedName::toString, stringComparator); return Comparator .comparing(PyImportElement::getImportedQName, Comparator.nullsFirst(qNamesComparator)) .thenComparing(PyImportElement::getAsName, Comparator.nullsFirst(stringComparator)); }