How to sort a list of objects with some specific property

前端 未结 4 1539
-上瘾入骨i
-上瘾入骨i 2021-01-20 13:38

I have a record object

 public Record{
   double simiADD;
 }

I Have a List of Record objects and I want to Sort on simiADD. Record with le

4条回答
  •  悲&欢浪女
    2021-01-20 13:44

    I assume that you meant that you don't want to implement "Comparable" interface in your class. Use Comparator and Collection's sort static method:

    import java.util.*;
    public class EmpSort {
        static final Comparator SENIORITY_ORDER =
                                     new Comparator() {
            public int compare(Employee e1, Employee e2) {
                return e2.hireDate().compareTo(e1.hireDate());
            }
        };
    
        // Employee database
        static final Collection employees = ... ;
    
        public static void main(String[] args) {
            Liste = new ArrayList(employees);
            Collections.sort(e, SENIORITY_ORDER);
            System.out.println(e);
        }
    }
    

提交回复
热议问题