0

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".

2
  • It looks like you have 3 different arrays. Which one of them do you want to sort, and how do they relate to each other? Commented Apr 11, 2020 at 21:38
  • sorry about that, i edited my question Commented Apr 11, 2020 at 21:44

1 Answer 1

3

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));
Sign up to request clarification or add additional context in comments.

2 Comments

that's incredible. Actually, I wanted the high score to be first, but if I print the list in reverse, it will not be a problem. thank you so much. Arrays.sort(FootballTeam, Comparator.comparing(Football::getPoint).reserved() is worked.
Yes the Comparator interface has utility methods for lots of things, like reversing the order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.