Collections.Sort意外地反转了“应该排序输出”

时间:2016-02-16 18:40:11

标签: java sorting collections

我根据3个标准​​对 // This builds the number of rows based on the number of lines in the file. while (line != null){ TableRow tblRow = new TableRow(this); tblRow.setPadding(5, 30, 5, 5); table.addView(tblRow); // iterates through the number of lines. // filling each cell with a button with the name of each part in the file. for (int col = 0; col < NUM_COL; col++) { Button btn = new Button(this); btn.setTextSize(14); btn.setText(line); btn.setOnClickListener(this); tblRow.addView(btn); line = reader.readLine(); } }; 进行排序:type - subtype - default flag, 现在一切正常,除了比较ArrayList时,当我在default Boolean flags方法上返回-1时,它实际上表现得像我返回Comparator.compare而反之亦然。

这是想要的订单:

1

我有3种不同类型的卡片:if E and R : show E before R if A and R : show R before A if E and A if A is default show A before E else show E before A E - R - AR的类型)

我的卡代码:

E

所以我在这里填写清单:

public class Card {

    public enum Type {
        E("E-Type"),
        A("A-Type"),
        Other("Other");

        ....
    }

    private String cuid;
    private boolean isDefault;
    private String ref;
    private Type type;
    ............

    // getters and setters

    public Boolean isDefault() {
        return isDefault;
    }

    public void setDefault(boolean isDefault) {
        this.isDefault = isDefault;
    }
}

这是我的比较器代码:

private static void populateCards () {
    Card eCard  = new Card ();
    Card rCard  = new Card ();
    Card aCard  = new Card ();

    eCard.setCuid ("eCard");
    rCard.setCuid ("rCard");
    aCard.setCuid ("aCard");

    eCard.setType (Card.Type.E);
    rCard.setType (Card.Type.E);
    aCard.setType (Card.Type.A);

    rCard.setRef ("ref");

    aCard.setDefault (true);

    // failing sorting case only with these 2 
    cards.add (aCard);
    cards.add (eCard);
}

所以我的问题与此代码有关:

Comparator<Card> comparator = new Comparator<Card> () {

    @Override
    public int compare (Card card1, Card card2) {

        if (cards.size () < 2) {
            return 0;
        }

        if (cards.size () == 2) {
            // E vs R
            if (card1.getType ().equals (card2.getType ())) {                       
                if (card1.getRef () == null) {
                    return -1;
                }
                return 1;
            }

            // if (R vs A) else (E vs A)
            if (card1.getRef != null || card2.getRef () != null) {
                return - card1.getType ().toString ().compareTo (card2.getType ().toString ());
            } else {
                if (card1.isDefault ()) {
                    // comes here successfully when card1 is Default
                    // ISSUE HERE it processes 1 as : show card2 before card1
                    return 1;
                } else if (card2.isDefault ()) {
                    // comes here successfully when card2 is Default
                    // ISSUE HERE it processes -1 as : show card1 before card2
                    return -1;
                } else {
                    return - card1.getType ().toString ().compareTo (card2.getType ().toString ());
                }
            }
        }

        ......................
    }
};

Collections.sort (cards, comparator);

为什么它将if (card1.isDefault ()) { // comes here successfully when card1 is Default // ISSUE HERE it processes 1 as : show card2 before card1 return 1; } else if (card2.isDefault ()) { // comes here successfully when card2 is Default // ISSUE HERE it processes -1 as : show card1 before card2 return -1; } else { return - card1.getType ().toString ().compareTo (card2.getType ().toString ()); } 1视为与它们应该相反的对象?

感谢。

0 个答案:

没有答案