我有一个基于不同运动类型的球员arrayList。我需要按照姓氏对球员列表进行排序。如果两个球员有相同的姓氏,则需要按照名字对这两个球员进行排序。 例如:格式为 姓 名字
Williams Robert
Phillips Warren
Doe John
Phillips Mark
输出结果应该为:
Doe John
Phillips Mark
Phillips Warren
Williams Robert
目前我只能按照名字的首字母或者姓氏进行排序。在我的代码中,我使用的是按照姓氏进行排序。
public static void sortPlayers(ArrayList playerList) {
for (int i = 0; i < playerList.size(); i++) {
for (int j = 0; j < playerList.size(); j++) {
Collections.sort(playerList, new Comparator() {
public int compare(Object o1, Object o2) {
PlayerStats p1 = (PlayerStats) o1;
PlayerStats p2 = (PlayerStats) o2;
return p1.getPlayerLastName().compareToIgnoreCase(p2.getPlayerLastName());
}
});
}
}
}