Home > Mobile >  can list.stream().sorted do with three canditions?
can list.stream().sorted do with three canditions?

Time:07-22

I already sucess sort by follow implementation,

if 'category' field equals, then compare 'sort' field, if 'category' field not equals, then compare 'id' field.

List<Contact> originContactList = ...;

originContactList.sort((contact1, contact2) -> {
    if (contact1.getCategory().compareTo(contact2.getCategory()) == 0) {
        return contact1.getSort().compareTo(contact2.getSort());
    } else {
        return contact1.getId().compareTo(contact2.getId());
    }
});

I want use java8 list.stream().sorted do the same as above code. Can it ?

I can't do if 'category' field not equals, then compare 'id' field.

List<Contact> originContactList = ...;

originContactList.stream().sorted(
        Comparator
                .comparing(Contact::getCategory)
                .thenComparingLong(Contact::getSort)
)
...

CodePudding user response:

You can use the comparator from your first example with streams too:

List<Contact> originContactList = ...;

originContactList.stream().sorted(
    (contact1, contact2) -> {
    if (contact1.getCategory().compareTo(contact2.getCategory()) == 0) {
        return contact1.getSort().compareTo(contact2.getSort());
    } else {
        return contact1.getId().compareTo(contact2.getId());
    }
})

But: any comparator that you write has to obey one basic rule:

If it says that from three contacts contactA, contactB and contactC the contactA should be sorted before contactB and contactB should be sorted before contactC then it must also say that contactA should be sorted before contactC.

Or in the words of the JavaDoc of Comparator.compare():

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0

With your comparator implementation it is easy to create three contacts that violate this rule.

  • Related