【问题标题】:Why sort method doesn't work for arraylist object为什么排序方法不适用于arraylist对象
【发布时间】:2019-07-25 06:54:26
【问题描述】:

我想根据 CompareTo() 方法使用 Collections.sort() 对数组列表进行排序

这是我的数组列表对象:

ArrayList<Personne> personnes = new ArrayList<Personne>();
        Personne p1 = new Personne("001590842","51862499", "N5+", "1", "20170201","0");
        Personne p2 = new Personne("001590842","51862499", "X0", "1", "20150529", "1");
        Personne p3 = new Personne("001639055","51862517", "G3", "1", "20170201", "2");
        Personne p4 = new Personne("001639055","51862517", "G3", "1", "20170201", "2");
        Personne p5 = new Personne("001597135","51862517", "G3", "1", "20170201", "2");
        Personne p6 = new Personne("001597135","51862517", "G3", "1", "20170201", "2");
        Personne p7 = new Personne("002804935","00006178","G4","1","19870101","1");
        Personne p8 = new Personne("002804935","00009118","X0","1","19861201","1");
        Personne p9 = new Personne("002804935","00009957","N4+","1","19861229","1");
        Personne p10 = new Personne("002804935","00012970","B3++","1","20100227","1");
        personnes.add(p1);
        personnes.add(p2);
        personnes.add(p3);
        personnes.add(p4);
        personnes.add(p5);
        personnes.add(p6);
        personnes.add(p7);
        personnes.add(p8);
        personnes.add(p9);
        personnes.add(p10);

这是我的 compareTo 函数:

public int compareTo(Object personne) {
        int res = 0;

        Personne other = (Personne) personne;

        // Conversion of Dates from String to Dates

        Date otherDate = converteDate(other.getDA_PRM_CTR_ORDER());
        Date entreePersonne = converteDate(this.DA_PRM_CTR_ORDER);
        res = entreePersonne.compareTo(otherDate);        

        // if there is Legality between dates 
        if (res == 0) {

            Long entreePersonneIDT = Long.parseLong(this.getIDT_ETT_PSE());
            Long otherPersonneIDT = Long.parseLong(other.getIDT_ETT_PSE());
            res = entreePersonneIDT.compareTo(otherPersonneIDT);
            return res;
        }
        return res;
    }

现在当我想打电话时

private static String SelectionCodeNote(ArrayList<Personne> listPersonnes) {

        if (null != listPersonnes) {
    for(Personne personne: listPersonnes)
                {
if (personne.getIDC_PSE_PCL().equals("1") && personne.getIDC_CD_NOT().equals("0")) {
                    return (personne.getCD_NOT());
                } else {
                    Collections.sort(listPersonnes);
                    return (personne.getCD_NOT());
                }
            }
}return null;
}

Personne 对象定义为

public class Personne {

    private String IDT_GCT;
    private String IDC_PSE_PCL;
    private String IDC_CD_NOT;
    private String DA_PRM_CTR_ORDER;
    private String IDT_ETT_PSE;
    private String CD_NOT;

    public Personne(String IDT_GCT, String IDC_PSE_PCL, String IDC_CD_NOT,
                    String DA_PRM_CTR_ORDER, String IDT_ETT_PSE, String CD_NOT) {
        this.IDT_GCT =  IDT_GCT;
        this.IDC_PSE_PCL = IDC_PSE_PCL;
        this.IDC_CD_NOT = IDC_CD_NOT;
        this.DA_PRM_CTR_ORDER = DA_PRM_CTR_ORDER;
        this.IDT_ETT_PSE = IDT_ETT_PSE;
        this.CD_NOT = CD_NOT;
    }

    public String getIDC_CD_NOT() {
        return this.IDC_CD_NOT;
    }

    public String getIDC_PSE_PCL() {
        return this.IDC_PSE_PCL;
    }

    public String getDA_PRM_CTR_ORDER() {
        return this.DA_PRM_CTR_ORDER;
    }

    public String getIDT_ETT_PSE() {
        return this.IDT_ETT_PSE;
    }

    public String getCD_NOT() {
        return this.CD_NOT;
    }

    public String getIDT_GCT() {
        return this.IDT_GCT;
    }
}

问题出在Collections.sort(listPersonnes);这一行

Collections 类型中的方法 sort(List) 不适用于 参数(ArrayList)

这不是很奇怪吗?

【问题讨论】:

  • 尝试在您的 Personne 类中实现 Comparable
  • 在哪里你在哪里实现compareTo?它似乎在Personne 之外,那么对Collections.sort 的调用如何能够访问它?你可能想调用重载Collections#sort(Collection&lt;E&gt;, Comparator&lt;E&gt;)

标签: java


【解决方案1】:

尝试在你的 Personne 类中添加Comparable,这样:

public class Personne implements java.lang.Comparable<Personne>
{ ... }

此外,正如评论中所建议的,您的 compareTo() 方法需要在您的 Personne 类中。


替代方案: 你可以这样做(在比较器的compare 方法中添加你的compareTo 方法):

Collections.sort(personnes, new Comparator<Personne>() {
        @Override
        public int compare(Personne o1, Personne o2) {
            int res = 0;

            Personne other = (Personne) personne;

            // Conversion of Dates from String to Dates

            Date otherDate = converteDate(other.getDA_PRM_CTR_ORDER());
            Date entreePersonne = converteDate(this.DA_PRM_CTR_ORDER);
            res = entreePersonne.compareTo(otherDate);        

            // if there is Legality between dates 
            if (res == 0) {

                Long entreePersonneIDT = Long.parseLong(this.getIDT_ETT_PSE());
                Long otherPersonneIDT = Long.parseLong(other.getIDT_ETT_PSE());
                res = entreePersonneIDT.compareTo(otherPersonneIDT);
                return res;
            }
            return res;
        }
    });

【讨论】:

    【解决方案2】:

    您需要在 Personne 上实现 Comparable 接口以覆盖 compareTo 逻辑,或者从 java-8 中您可以使用 lambda 表达式进行自定义比较器

    Comparator<Personne> c = (p1,p2)-> {
    
                   int res = 0;
        // Conversion of Dates from String to Dates
    
        Date otherDate = converteDate(p1.getDA_PRM_CTR_ORDER());
        Date entreePersonne = converteDate(p2.DA_PRM_CTR_ORDER);
        res = entreePersonne.compareTo(otherDate);        
    
        // if there is Legality between dates 
        if (res == 0) {
    
            Long entreePersonneIDT = Long.parseLong(p1.getIDT_ETT_PSE());
            Long otherPersonneIDT = Long.parseLong(p2.getIDT_ETT_PSE());
            res = entreePersonneIDT.compareTo(otherPersonneIDT);
            return res;
        }
        return res;
      };
    

    你也可以使用ArrayListsort方法

    listPersonnes.sort(c);
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多