After java8, you can sort a list by a field as follows:

 res.stream().sorted(Comparator.comparing(RuralEnterpriseInfo::getEndTime).reversed()).collect(Collectors.toList());
Copy the code

And then we know

RuralEnterpriseInfo::getEndTime
Copy the code

It can be replaced by the following

p -> p.getEndTime()
Copy the code

Then modify the code as follows

res.stream().sorted(Comparator.comparing( p -> p.getEndTime()).reversed()).collect(Collectors.toList());
Copy the code

The code is reporting an error at this point



However, we can remove reversed code without reporting an error



We know from looking at the Comparing method and the reversed method that reversed returns a generic type, and comparing requires a function which requires an object in this case, so let’s convert it

res.stream().sorted(Comparator.comparing( (RuralEnterpriseInfo p) -> p.getEndTime()).reversed()).collect(Collectors.toList());
Copy the code

So you can