How to compare objects by multiple fields

后端 未结 22 2541
暖寄归人
暖寄归人 2020-11-22 00:43

Assume you have some objects which have several fields they can be compared by:

public class Person {

    private String firstName;
    private String lastN         


        
22条回答
  •  有刺的猬
    2020-11-22 01:27

    You can also have a look at Enum that implements Comparator.

    http://tobega.blogspot.com/2008/05/beautiful-enums.html

    e.g.

    Collections.sort(myChildren, Child.Order.ByAge.descending());
    

提交回复
热议问题