简体   繁体   English

TreeSet无法使用自定义比较器正确排序

[英]TreeSet is not sorting correctly with custom comparator

I have a customized TreeSet: 我有一个自定义的TreeSet:

    TreeSet<String> sortedOptionSet = new TreeSet<String> (new Comparator<String>() {

        @Override
        public int compare(String arg1, String arg2) {
            if (arg1 == null || arg2 == null) {
                return -1;
            }

            String o1 = arg1, o2 = arg2;
            String k1 = "1", k2 = "2";

            try{
                if (o1.contains("P1")) {
                    return -1;
                }
                else if (o2.contains("P1")) {
                    return 1;
                }
                else if (o1.contains("P2")) {
                    return -1;
                }
                else if (o2.contains("P2")) {
                    return 1;
                }
            } catch (Exception e) {}


            //If there is no rule for this combination, order them using key number ascending
            int result = 1;
            try {
                Integer key1 = Integer.valueOf(k1);
                Integer key2 = Integer.valueOf(k2);
                result = key1.compareTo(key2);
                if (result == 0) {
                    result = 1;
                }
            } catch (Exception e) {
            }
            return result;

        }
    });

    sortedOptionSet.add("TEST1");
    sortedOptionSet.add("P1");
    sortedOptionSet.add("TEST2");
    sortedOptionSet.add("P2");
    sortedOptionSet.add("TEST3");

    ArrayList<String> result = new ArrayList<String>();
    result.addAll(sortedOptionSet);

    for (String s : result) {
        System.out.println(s);
    }

I made the condition of P1 come first and thought the result should be: 我首先确定了P1的条件,并认为结果应该是:

P1
P2
TEST1
TEST2
TEST3

But the result returns 但是结果返回

P2
P1
TEST1
TEST2
TEST3

I could not possible figure out why this is the behavior. 我无法弄清楚为什么这是行为。 Please help. 请帮忙。

Your comparator violates many rules of the contract. 您的比较者违反了合同的许多规则。 In particular, a comparator is supposed to be consistent: 特别是,比较器应该是一致的:

A > B iff B < A
A = B iff B = A
A > B and B > C ==> A > C

That's not the case. 事实并非如此。 For example, if A and B are both null, compare(A, B) will lead to A < B, and compare(B, A) will lead to B < A. 例如,如果A和B都为空,则compare(A, B)将导致A <B,而compare(B, A)将导致B <A。

Same if A and B both contain P1 or P2. 如果A和B都包含P1或P2,则相同。

And if they have the same integer key, compare(A, B) will lead to A > B, and compare(B, A) will lead to B > A. Except in that case, you're not even comparing the arguments of the comparator, but two hard-coded values k1 and k2. 如果它们具有相同的整数键,则compare(A, B)会导致A> B,而compare(B, A)会导致B>A。除了那种情况,您甚至都没有比较比较器,但有两个硬编码值k1和k2。 And if there is an exception parsing any of the number, the first one is always the biggest one. 而且,如果有解析任何数字的异常,则第一个数字始终是最大的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM