What is the correct way to sort a list using a Java lambda comparator, and how can I fix the type mismatch error?

If you’re absolutely sure the values returned by getTime() fit safely within an int range (which is rare with timestamps), you could cast them.

But this is not recommended for general use:

messagesByDeviceType.sort((o1, o2) -> (int)(o1.getTime() - o2.getTime()));