1

Ok so I'm kind of in the loss here but here goes. So I need to sort the array medalList when they get printed out. First I need to sort by gold medals which are added to the index [0], second after silvers in index [1], third after bronze in index [2] and last if a team is tied they get sorted by team name. Do I need to call a sorting method in another class, keep track of one and sort through and compare to the rest of the teams and if they're the best print them out first?

How do I compare Integers in an array of one classes to another?

When a user enters a certain command a list of teams with their results will get printed out.

As of now it looks like this:

1st 2nd 3rd  Team Name
0   0     0      North Korea
3   1     1      America
5   0     2      France
2   1     3      Germany

I want it to say:

1st 2nd 3rd  Team Name
5   0     2  France
3   1     1  America
2   1     3  Germany
0   0     0  North Korea
import java.util.ArrayList;
import java.util.Arrays;

public class Team {
    private String teamName;
    private ArrayList<Participant> participantList = new ArrayList<Participant>();
    private int[] medalList = new int[3];

    public Team(String teamName) {
        this.teamName = teamName;
    }

    public String getTeamName() {
        return teamName;
    }

    public void addParticipant(Participant participant) {
        participantList.add(participant);
    }

    public void removeFromTeam(int participantNr){
        for(int i = 0; i < participantList.size(); i++){
            if(participantList.get(i).getParticipantNr() == participantNr){
                participantList.remove(i);
            }
        }
    }

    public void printOutParticipant() {
        for(int i = 0; i < participantList.size(); i++){
            System.out.println(participantList.get(i).getName() + " " + participantList.get(i).getLastName());
        }
    }

    public boolean isEmpty() {
        boolean empty = false;
        if (participantList.size() == 0) {
            empty = true;
            return empty;
        }
        return empty;
    }

    public void emptyMedalList(){
        Arrays.fill(medalList, 0);
    }

    public void recieveMedals(int medal) {
        if(medal == 1){
            int gold = 0;
            gold = medalList[0];
            medalList[0] = ++gold;
        } else if (medal == 2){
            int silver = 0;
            silver = medalList[1];
            medalList[1] = ++silver;
        } else if (medal == 3){
            int bronze = 0;
            bronze = medalList[2];
            medalList[2] = ++bronze;
        }
    }

    public void printMedals(){
        System.out.println(medalList[0] + "   " + medalList[1] + "   " + medalList[2] + "   " + teamName);
    }

   public int compareTo(Team team) {
    int goldDif = Integer.compare(team.medalList[0], this.medalList[0]);
    if (goldDif != 0)
            return goldDif;
    int silverDif = Integer.compare(team.medalList[1], this.medalList[1]);
    if (silverDif != 0)
        return silverDif;
    int bronzeDif = Integer.compare(team.medalList[2], this.medalList[2]);
    if (bronzeDif != 0)
        return bronzeDif;
    return this.getTeamName().compareTo(team.getTeamName());
}

    public String toString() {
        return teamName;
    }

}
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
Fresh Java
  • 107
  • 1
  • 10
  • 1
    I'm sorry, you're question didn't really make sense. Can you provide an example for us to follow? – m_callens Jan 27 '17 at 19:45
  • 1
    Show the code where you're trying to sort. Also, you would have to expose `medalList` in some form if you want to sort outside this class. – shmosel Jan 27 '17 at 19:48
  • I Didn't have time to edit, I was supposed to type it out like this. Oh sorry, I was typing while trying to figure out the code. Should have taken more time to come up with a proper question. When a user enters a certain command a list of teams with their results will get printed out. As of now it looks like this: http://i.imgur.com/sJQpgsQ.png I want it to say: http://i.imgur.com/bB9gha0.png – Fresh Java Jan 27 '17 at 20:02
  • Please don't use screenshots for text. – Chai T. Rex Feb 04 '17 at 01:57

1 Answers1

1

Make your Team class comparable

public class Team implements Comparable<Team> {

and add a comparison method

@Override
public int compareTo(final Team other) {
    for (int i = 0; i < 3; i++) {
        final int compareMedals = Integer.compare(medalList[i], other.medalList[i])
        if (compareMedals != 0) {
            return compareMedals;
        }
    }
    return teamName.compareTo(other.teamName);
}

This will check gold medals first, then silver medals if the amount of gold medals is equal and so on and use the team name comparison as a last resort. You can then sort a collection of Teams with

final List<Team> teams = new ArrayList<>();
...
Collections.sort(teams);

EDIT:

Or if you like it in Java 8 style you could also write your comparison method like

@Override
public int compareTo(final Team other) {
    return Stream.of(0, 1, 2)
                .map(i -> Integer.compare(medalList[i], other.medalList[i]))
                .filter(i -> i != 0)
                .findFirst()
                .orElse(teamName.compareTo(other.teamName));
}
Florian Link
  • 638
  • 4
  • 11
  • 1. Why do you like `final` so much? A good practice I am not aware of? – Grzegorz Górkiewicz Jan 27 '17 at 20:12
  • You can give http://stackoverflow.com/questions/500508/why-should-i-use-the-keyword-final-on-a-method-parameter-in-java a read for starters. In short, it is a good practice to make everything `final` unless you really need it to be reassignable. That especially applies to method parameters in Java. – Florian Link Jan 27 '17 at 20:16
  • 1
    Still sounds more like a matter of taste rather than a good practice. – Grzegorz Górkiewicz Jan 27 '17 at 20:19
  • Unless you mean by taste if code is more error-prone or less error-prone I have to disagree because it has functional differences. However, that discussion has been made elsewhere, you can just google it if you like. – Florian Link Jan 27 '17 at 20:24
  • I agree that it's less error-prone. But ultimately it still boils down to preference and convention, and it's definitely the less common practice. Personally, I avoid it because I don't like the verbosity it adds. – shmosel Jan 27 '17 at 20:43
  • Nothing happens, my list still prints out the same. Do I have to change my printMedals() method? Because that's the one that gets called in the other class. – Fresh Java Jan 27 '17 at 20:52
  • 1
    Just for fun, here's an alternative Java 8 approach: `return IntStream.range(0, 3).mapToObj(i -> Comparator.comparingInt((Team t) -> t.medalList[i])).reduce(Comparator::thenComparing).get().thenComparing(Comparator.comparing(t -> t.teamName)).compare(this, other);` – shmosel Jan 27 '17 at 20:53