2015-01-26 81 views
1

所以,我不知道是否有一个优雅的解决方案,但这里。我想排序列表,但列表包含三种类型的项目。我想要的是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列表只有一半排序。所以我假设我的代码中有一个错误。让我知道你是否需要更多信息

编辑2:做了一些更多的样本测试,它看起来像字符串根本没有被排序。我做了两个

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

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

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

+0

您的字母顺序排序不会按照您指定的方式进行。 String.compareTo(String)使用词典排序。您可能需要考虑区分大小写。 对不起,如果这使得它稍微优雅看看。 – 2015-01-26 22:16:49

+0

@StephenSouness我认为这不重要。它应该正确地对字符串的排序方式进行排序 – Sree 2015-01-27 00:04:34

+0

好的。没有更多的上下文很难说清楚,但是如果你乐于让Zebra在食物之前展示(例如),那么这就是你的决定。 – 2015-01-27 00:15:09

回答

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

@sree如果你想revenrse命令做'another.getUsername()。compareTo(this.getUsername())'你能否也提供一些示例数据 – user902383 2015-01-27 09:44:19

+0

我做了你的建议并收集了更多的数据。请参阅我的编辑,似乎字符串没有被排序 – Sree 2015-01-27 15:50:19

+0

@Sree可以提供一些输入和输出示例,因为对我来说它工作正常http://ideone.com/5BsqqJ – user902383 2015-01-27 16:43:50

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

我喜欢你的代码有多简单。但请看我的编辑。我很抱歉,我应该更好地解释自己。您是否还需要在程序中对两种不同的条件进行排序? – Sree 2015-01-27 00:07:22

+0

@Sree是的,它的工作原理。可能'getType()。equals(TypeA)'不能像你期望的那样工作。 'getUsername()'是一个常规字符串(或实现Comparable的复杂对象)? – fRoStBiT 2015-01-27 06:18:20

+0

TypeA只是一个枚举,用户名只是一个字符串 – Sree 2015-01-27 15:31:37

1

你一定要实现你的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 
} 
+0

请参阅我的编辑。我认为你的代码和我的基本逻辑不会改变吗? – Sree 2015-01-27 00:06:10

1

有一个优雅的方式来解决这个问题,它不涉及丑陋的compareTo trainwrecks。

  1. 通过您名单,并2 SortedSet,一个为A,一个用于B + C。根据他们的类型添加你的友谊。
  2. 创建一个新列表并使用Collections.addAll()方法在列表中追加2个阵列,您可以从2个SortedSet获得第一个,第一个为A,然后为B+C

由于SortedSet将保持内容的自然秩序,这是辞书的字符串,最终名单将有类型A第一,按字典顺序排序,B and C后,也会分类字典序。

+0

我想到了这一点,但希望不要这样做,因为它似乎不雅。你不这么认为吗? – Sree 2015-01-27 00:05:33

+0

与所有这些超级丑陋的比较器相比,这对我来说似乎相当干净。没有if(...)elseif(...)else {if(....)elsif(....)else {...}} = big win。 – Dave 2015-01-27 00:07:34

+0

因此,为应用程序创建一个单一的比较器,您将创建3个列表,实际上“非常干净” – user902383 2015-01-27 16:47:09

相关问题