繁体   English   中英

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

[英]Sort a list based on two conditions

因此,我不知道是否有一个优雅的解决方案,但是可以解决。 我想对列表进行排序,但是列表包含三种类型的项目。 我想要的是类型A在顶部按字母顺序排序,类型B和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&C列表仅排序一半。 所以我假设我的代码中有一个错误。 让我知道您是否需要更多信息

EDIT2:对样本进行了更多测试,看起来字符串根本没有排序。 我都做过

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

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

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

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

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());
        }

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

但是代码可以更短:

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());
    }
}

您只需要使用您说的逻辑在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
}

有一个优雅的方法可以解决此问题,并且它不涉及难看的compareTo训练残骸。

  1. 通过您的列表,并进行2 SortedSet ,一个用于A和一个用于B + C 根据好友类型添加好友。
  2. 新建一个列表,并使用Collections.addAll()方法将从2 SortedSet获得的2个数组附加到列表中,第一个数组用于A ,然后一个数组用于B+C

由于SortedSet将保持内容的自然顺序,即按字典顺序对字符串进行排序,因此您的最终列表将首先按字典顺序对Type A进行排序,之后按字典顺序对B and C进行排序。

暂无
暂无

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

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