简体   繁体   English

(Java)如何对对象数组进行排序,并断开两个对象之间的联系?

[英](Java) How can I sort an array of objects, and break ties between two objects?

Say I have an object - Team . 说我有一个对象 - Team Team has three fields, wins , losses and draws . Team有三个领域, winslossesdraws

If I have a few objects say Team1 , Team2 , Team3 , where Team1 has 3 wins, 2 losses, and 1 tie, Team2 has 3 wins and 3 losses, and Team3 has 2 wins, 3 losses, and 1 tie. 如果我有一些对象说Team1Team2Team3 ,其中Team1有3胜2负1分, Team2有3胜3负, Team3有2胜3负1分。

Put all three teams in an array. 将所有三个团队放在一个数组中。

I am trying to sort (I know how to do this.. implement Comparable , override compareTo() use Array.sort ) them by wins, and if two teams have the same wins, I want to then sort them by losses (and eventually a third field will be added to further break ties). 我试图排序(我知道如何做到这一点..实现Comparable ,覆盖compareTo()使用Array.sort )他们的胜利,如果两个团队有相同的胜利,我想然后按损失排序(最终将添加第三个字段以进一步打破关系)。

Should I write my own sort method? 我应该编写自己的排序方法吗? Can someone point me in the right direction because I have no clue where to go with this. 有人能指出我正确的方向,因为我不知道该怎么做。

Similar to Ben's answer, but a bit different, I often use the following pattern for clarity. 与Ben的答案类似,但有点不同,为了清晰起见,我经常使用以下模式。 This is a Comparator , but the code for Comparable would be similar. 这是比较器 ,但Comparable的代码类似。

Comparator<Team> myComparator = new Comparator() {
   @Override
   public int compare(Team t1, Team t2) {
      int result = t1.getWins() - t2.getWins();
      if (result == 0)
         result = t2.getLosses() - t1.getLosses();
      if (result == 0)
         ... more tests here

      return result;
   }

};

Note that taking the difference of two integers might overflow in extreme cases, so a more robust variation would use Integer.compare(t1.getWins(), t2.getWins()) . 请注意,在极端情况下,取两个整数的差异可能会溢出,因此更强大的变体将使用Integer.compare(t1.getWins(), t2.getWins()) However, in this case, it is unlikely that your teams will have more than 2^31 wins or losses. 但是,在这种情况下,您的团队不太可能有超过2 ^ 31的胜负。 :-) :-)

To use this, go 要使用它,去吧

Arrays.sort(myArrayOfTeams, myComparator);

If you have the good fortune to be using , then you could just do this: 如果您有幸使用 ,那么您可以这样做:

ArrayList<Team> teams = myTeams();

Collections.sort(teams,
    Comparator.comparingInt(Team::getWins())
              .thenComparingInt(Team::getTies());
    );

Or if you are using an array: 或者如果您使用的是数组:

Team[] teams = myTeams();

Arrays.sort(teams,
    Comparator.comparingInt(Team::getWins())
              .thenComparingInt(Team::getTies());
    );

This also makes it easier to sort by other things, such as the team's name. 这样也可以更容易地按其他方式进行排序,例如团队名称。

You need to modify Comparable to not only consider wins, but to consider ties, especially when wins occur. 您需要修改Comparable,不仅要考虑胜利,还要考虑关系,特别是在胜利发生时。

So in your compareTo, add an if to check when wins are equal, and then inside that if do the check on losses. 所以在你的compareTo中,添加一个if来检查胜利是否相等,然后在里面检查是否检查损失。 Here you can even add in the check for draws as well. 在这里你甚至可以添加抽签检查。

In your overridden compareTo() method, you would simply need to catch when the wins are equal and then assign it a preference based on losses so 在重写的compareTo()方法中,您只需要在胜利相等时捕获,然后根据损失为其分配偏好,以便

compareTo(Team other){
    if(this.getWins() > other.getWins()){
        return 1;
    } else if(this.getWins() == other.getWins()){
        if(this.getLosses() < other.getLosses()){
            return 1;
        }
        return -1;
    }
    return -1;
}

You could then add another else if in the inner loop to check if losses are equal, and in that if statement you would compare the ties, and if they are completely equal you would return a 0 然后你可以在内循环中添加另一个其他来检查损失是否相等,并在if语句中你将比较关系,如果它们完全相等,你将返回0

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

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