Skip to content

Add pattern for Comparator combinators #5888

Description

@electrum

IntelliJ has the ComparatorCombinators inspection.

Before:

myList.sort((person1, person2) -> person1.getName().compareTo(person2.getName()));
 
myList2.sort((person1, person2) -> {
    int res = person1.first().compareTo(person2.first());
    if(res == 0) res = person1.second().compareTo(person2.second());
    if(res == 0) res = person1.third() - person2.third();
    return res;
});

After:

myList.sort(Comparator.comparing(Person::getName));
 
myList2.sort(Comparator.comparing(Person::first)
                       .thenComparing(Person::second)
                       .thenComparingInt(Person::third));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions