这是我的VO

public class SomeVO {

    private String name;
    private String usageCount;
    private String numberofReturns;
    private String trendNumber;
    private String nonTrendNumber;
    private String trendType;
    private String auditType;
    public SomeVO(String name,String usageCount,String numberofReturns,String trendNumber,String nonTrendNumber,String trendType,String auditType){
        this.name = name;
        this.usageCount = usageCount;
        this.numberofReturns = numberofReturns;
        this.trendNumber = trendNumber;
        this.nonTrendNumber = nonTrendNumber;
        this.trendType = trendType;
        this.auditType = auditType;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUsageCount() {
        return usageCount;
    }
    public void setUsageCount(String usageCount) {
        this.usageCount = usageCount;
    }
    public String getNumberofReturns() {
        return numberofReturns;
    }
    public void setNumberofReturns(String numberofReturns) {
        this.numberofReturns = numberofReturns;
    }
    public String getTrendNumber() {
        return trendNumber;
    }
    public void setTrendNumber(String trendNumber) {
        this.trendNumber = trendNumber;
    }
    public String getNonTrendNumber() {
        return nonTrendNumber;
    }
    public void setNonTrendNumber(String nonTrendNumber) {
        this.nonTrendNumber = nonTrendNumber;
    }
    public String getTrendType() {
        return trendType;
    }
    public void setTrendType(String trendType) {
        this.trendType = trendType;
    }
    public String getAuditType() {
        return auditType;
    }
    public void setAuditType(String auditType) {
        this.auditType = auditType;
    }
}


这是我的价值观

List<SomeVO> myList = new ArrayList<SomeVO>();
        SomeVO some = new SomeVO("A","0","0","123","123","Trend","AuditX");
        myList.add(some);
        some = new SomeVO("B","1","1","234","234","Non trend","AuditX");
        myList.add(some);
        some = new SomeVO("C","0","2","345","345","Trend","AuditX");
        myList.add(some);
        some = new SomeVO("D","2","3","546","546","Trend","AuditX");
        myList.add(some);
        some = new SomeVO("E","2","4","678","678","Non trend","AuditX");
        myList.add(some);
        some = new SomeVO("F","0","0","123","123","Non trend","AuditA");
        myList.add(some);
        some = new SomeVO("G","0","0","123","123","Trend","AuditB");
        myList.add(some);


这是我的比较者

public String currentAudit = "AuditX";
public class AuditComparator implements Comparator<SomeVO> {

        @Override
        public int compare(SomeVO o1, SomeVO o2) {
            if(currentAudit.equalsIgnoreCase(o1.getAuditType()) && currentAudit.equalsIgnoreCase(o2.getAuditType())) {
                int value1 = o2.getUsageCount().compareTo(o1.getUsageCount());
                if (value1 == 0) {
                    int value2 = o1.getNumberofReturns().compareTo(o2.getNumberofReturns());
                    if(o1.getTrendType().equalsIgnoreCase("Trend") && o2.getTrendType().equalsIgnoreCase("Trend")) {
                        if (value2 == 0) {
                            return o1.getTrendNumber().compareTo(o2.getTrendNumber());
                        } else {
                            return value2;
                        }
                    } else {
                        if (value2 == 0) {
                            return o1.getNonTrendNumber().compareTo(o2.getNonTrendNumber());
                        } else {
                            return value2;
                        }
                    }
                }
                return value1;
            } else {
                return 1;
            }

        }
    }


我正在尝试根据以下条件对VO进行排序


仅应将currentAudit的第一组值用于
考虑因素,即AuditX

a)然后应与
    用法按降序计数

b)如果发现相同的使用次数,则它
    应按升序排序

c)如果相同
    返回计数,则应检查是否为TrendType,如果TrendType
    =“ Trend”,则应按趋势编号排序,否则应为非趋势编号。
那么它应该考虑休息所有的auditType并排序
a),b),c)条件,例如currentAudit。我试图实现它,我
最后只有上面的比较器。预期结果:D,A,C,E,
F,G。但是我得到了G,F,D,E,B,A,C。请帮助我更新
上面的比较器。

最佳答案

您的比较器不满足一个简单的条件:它不是无状态的。以下内容应始终为真:A>B => B<A。在您的情况下,在某些情况下A>B and B>A

关于java - 基于多个连接的订单数组列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35452225/

10-11 03:19