2014-11-15 145 views
0

我遇到了一些问题。我使用ArrayList来创建一个足球联盟表。现在我需要做的是使用ClubPoint对ArrayList进行排序。我尝试使用比较器,但不确定要从哪里去。任何帮助表示赞赏使用比较器排序ArrayList

ArrayList<FootballClub> newClub = new ArrayList<FootballClub>(); 

String format = "|%1$-15s|%2$-15s|%3$-15s|%4$-15s|%5$-15s|%6$-15s|%7$-15s|%8$-15s|%9$-15s|\n"; 
        System.out.format(format, "Score", "Name", "Location", "Wins", "Losses", "Draws", 
          "Goals Received", "Goals Scored", "Games Played"); 
        for(int x = 0; x < newClub.size(); x++){ 

        Collections.sort(newClub, new DescendingByPointsComparator()); 

        System.out.format(format, newClub.get(x).ClubScore, newClub.get(x).ClubName, newClub.get(x).ClubLocation, 
          newClub.get(x).ClubWins, newClub.get(x).ClubLosses, newClub.get(x).ClubDraws, newClub.get(x).GoalsReceived, 
          newClub.get(x).GoalsScored, newClub.get(x).ClubGamesPlayed); 

        } 


class DescendingByPointsComparator implements Comparator<FootballClub> { 
    @Override 
    public int compare(FootballClub lhs, FootballClub rhs) { 
     return Integer.compare(rhs.getPoints(), lhs.getPoints()); 
    } 
} 
+1

是什么你现在有什么不对? –

+0

我在类DescendingByPointsComparator“Can not Find Symbol:method getPoints()”中得到一个错误 –

+0

然后“FootballClub”的定义似乎是相关的,不是吗? –

回答

0

在你FootballClub模型类,则可以覆盖下面的方法来实现你想要的:

public int compareTo(FootballClub anotherFootballClub){ 

    // Your comparison logic here 
    return //negative integer if this instance is lesser and vice versa 
} 

此外,请确保您的FootballClub类实现Comparable

+0

之外进行排序。 –