根据两个条件对列表进行排序

时间:2015-01-26 21:33:32

标签: java android sorting

所以,我不知道是否有一个优雅的解决方案,但这里。我想对列表进行排序,但列表包含三种类型的项目。我想要的是A型按字母顺序排序,键入B&a​​mp; C位于底部并按字母顺序排序(类型B和C将合并)。

这是我的代码:

public int compareTo(Friendship another) {
    if(this.getType().equals(TypeA) &&
            another.getType().equals(TypeA)){ //if they are both type A, just sort based on user name

        return this.getUsername().compareTo(
                another.getUsername());
    }
    else if(this.getType.equals(TypeA)){ 
        return -1;
    }
    else if(another.getType().equals(TypeA)){
        return 1;
    }
    else{ //this will be hit if they are either Type B or C, then just sort based on username
        return this.getUsername().compareTo(
                another.getUsername());
    }
}
编辑:抱歉,我应该更好地解释这一点。问题是上面的代码不起作用。从我所看到的,该列表似乎没有正确订购。 <击> TypeA列表出于某种原因与我想要的相反(Z - > A)。和TypeB&amp; C列表只有一半排序。所以我假设我的代码中存在错误。如果您需要更多信息,请告诉我。

EDIT2:对样本做了一些测试,看起来字符串根本没有排序。我做了两件事

this.getUsername().compareTo(
            another.getUsername());

another.getUsername().compareTo(
            this.getUsername());

编辑3:你们是对的。我的代码中的其他地方有一个错误(这是无关的)。对不起......在这种情况下,我也不知道该怎么做。我该给谁正确答案?

4 个答案:

答案 0 :(得分:2)

如果我是你,我不会改变结构,但只是优化这一点

public int compareTo(Friendship another) {


       if(!this.getType().equals(another.getType()){
        //if type are not equal, so we might have at most one A

          if(this.getType.equals(TypeA)){ //on left side
             return -1;
          }

          if(another.getType().equals(TypeA)){ //or, on rightside
            return 1;
          }
        }
          //or we have on both sides or neither side
            return this.getUsername().compareTo(
                    another.getUsername());
        }

答案 1 :(得分:1)

我在相同的情况下使用类似的解决方案,我认为这很好。

但代码可以更短:

public int compareTo(Friendship another) {
    boolean thisOnTop = getType().equals(TypeA);
    boolean anotherOnTop = another.getType().equals(TypeA);
    if (thisOnTop != anotherOnTop) {
        return thisOnTop ? -1 : 1;
    } else {
        return this.getUsername().compareTo(another.getUsername());
    }
}

答案 2 :(得分:1)

你只需要用你说过的逻辑在3个类中实现你的compareTo。像这样:

// TypeA.class
// TypeA class will have priority over the other two, so just sort by whatever you want
public int compareTo(AnotherType anotherType) {
    if (this.equals(anotherType)) // TypeA vs TypeA - alphabetically
        return this.getUsername().compareTo(anotherType.getUsername());
    else // otherwise typeA is greater
        return 1; // 1 means greater than
}

// TypeB.class
public int compareTo(AnotherType anotherType) {
    if (this.equals(anotherType)) // both typeB, sort alphabetically
        return this.getUsername().compareTo(anotherType.getUsername());
    else
        if(this.equals(typeC)) // TypeB vs TypeC, alphabetically
            return this.getUsername().compareTo(typeC.getUsername());
        else // TypeB vs TypeA
            return -1; // -1 means lesser than
}

//TypeC.class
public int compareTo(AnotherType anotherType) {
    if (this.equals(anotherType)) // TypeC vs TypeC - alphabetically
        return this.getUsername().compareTo(anotherType.getUsername());
    else
    if(this.equals(typeB)) // TypeC vs TypeB - alphabetically
        return this.getUsername().compareTo(typeB.getUsername());
    else
        return -1; // -1 means lesser than
}

答案 3 :(得分:1)

有一种优雅的方法可以解决这个问题,它并不涉及丑陋的比较火车残骸。

  1. 通过您的列表并制作2 SortedSet,一个用于A,另一个用于B + C。根据类型添加友谊。
  2. 制作一个新列表,并使用Collections.addAll()方法向列表中添加您可以从2 SortedSet获得的2个数组,首先是A的数组,然后是B+C的数组。 {1}}。
  3. 由于SortedSet将保持内容的自然顺序,这是字符串的词典,因此您的最终列表将首先使用类型A,按字典顺序排序,然后按B and C排序,并按字典顺序排序。

相关问题