你好,
我有一个 OBJECTS(团队)的 ArrayList。他们是足球队,我想通过他们的得分来对他们进行分类。显然,我完全知道 ArrayList 是按照插入顺序排序的。
但是我有一个 TEAM 类,其中包含 gamesWon、gamesLost、gameTied 和基于比赛结果的分数。
我已经准备好了一切,但我发现通过它的点对 ArrayList 进行排序将是一个很酷的功能。
我已经在线阅读了 Collections.sort(myArrayList),但是这是基于一种类型的变量进行排序,并且由于我在 ArrayList 中有对象,所以我想对此进行排序。
谢谢。
马可
最佳答案
您应该定义 Comparator
为此。
public class PointsBasedTeamComparator implements Comparator<Team> {
public int compare(Team t1, Team t2) {
//return the result of points comparison of t1 and t2
}
}
并按如下方式使用
Collections.sort(teams, new PointsBasedTeamComparator());
关于java - 通过其实例变量之一对对象的 ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446228/