Using Comparable for multiple dynamic fields of VO in java

后端 未结 7 1855
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 11:36

I have a class

public class StudentVO {
   int age;
   String name;  
}

I used the same class in two different areas. At one place i need t

7条回答
  •  再見小時候
    2020-11-28 12:05

    You can cascade the comparators with thenComparing:

    List files = new ArrayList();
    Collections.sort(files,
    
            new Comparator() {
                public int compare(File file1, File file2) {
                    return file2.getName()
                            .compareTo(file1.getName());
                }
            }.thenComparing(
                    new Comparator() {
                        public int compare(File file1, File file2) {
                            return Long.valueOf(file2.getPath().length())
                                    .compareTo(Long.valueOf(file1.getPath().length()));
                        }
                    }
            )
    
    );
    // Collections.reverse(list);
    

    https://www.eovao.com/en/a/sort%20elements%20java/4/how-to-sort-objects-in-java---multiple-comparison-sort-list

提交回复
热议问题