aby
aby

Reputation: 17

sorting array by part of array

I have an Object array like this

Football[] FootballTeam = new Football[4];
 FootballTeam[i] = new Football(ranked, names, played, w, d, l, GF, GA, GD,points);
 FootballTeam[0] = 0  Real Madrid 3   2   0   1   7:6 1   5
 FootballTeam[1] = 1  Barcelona   3   0   0   3   4:9 -5  1
 FootballTeam[2] = 2  Valencia    3   1   0   2   7:7 0   5
 FootballTeam[3] = 3  Atletico Madrid 3   3   0   0   9:5 4   7

how can I sort this list by point and average(GD) or How can I print in order sorted by score and average? . I can delete "ranked".

Upvotes: 0

Views: 86

Answers (1)

Joni
Joni

Reputation: 111239

You can use the Arrays.sort method, from java.util.Arrays class, by passing in a "comparator" object. The comparator should compare the attributes you want to sort by.

Assuming your Football class has methods that return the "points" and "GD", this will sort the teams by points, and the teams that have the same point count are sorted by "GD".

Arrays.sort(FootballTeam, Comparator.comparing(Football::getPoint)
                                    .thenComparing(Football::getGD));

Upvotes: 3

Related Questions