在我执行代码时,助攻最多的对象也位于最佳传球手和最佳得分手名单中。我该如何对目标最多的对象进行排序并分别进行辅助?
class AthleteStats {
int athleteId;
int goals;
int assists;
AthleteStats(this.athleteId, this.goals, this.assists);
}
class Athlete {
String name;
List<AthleteStats> stats;
Athlete(this.name, this.stats);
}
主要
void main() {
List<Athlete> players = new List<Athlete>();
players.add(new Athlete('Messi', [AthleteStats(10, 65, 32)]));
players.add(new Athlete('Ronaldo', [AthleteStats(7, 159, 20)]));
players.add(new Athlete('Neymar', [AthleteStats(10, 25, 35)]));
players.sort((a, b) =>
(b.stats.fold(0, (prev, element) => prev + element.goals))
.compareTo(a.stats.fold(0, (prev, element) => prev + element.goals)));
players.sort((a, b) =>
(b.stats.fold(0, (prev, element) => prev + element.assists))
.compareTo(a.stats.fold(0, (prev, element) => prev + element.assists)));
logTopGoals(players);
logTopPassers(players);
}
得分最高和得分最高的球员的打印功能
void logTopGoals(var lst) {
print('Top scorers: ');
lst.forEach((l) => print(
'${l.name}: ${l.stats.fold(0, (prev, element) => prev + element.goals)}'));
}
void logTopPassers(var lst) {
print('Top Passers: ');
lst.forEach((l) => print(
'${l.name}: ${l.stats.fold(0, (prev, element) => prev + element.assists)}'));
}
OutPut
Top scorers:
Neymar: 25
Messi: 65
Ronaldo: 159
Top Passers:
Neymar: 35
Messi: 32
Ronaldo: 20