3

I'm working on a Java project that asks me to implement the function obtainRanking() : void with the following description:

Sort the players list by using the class method sort (List) from the Collections class, a method to sort the objects of a collection. For that, the object's class (Player in our case), should implement the interface "Comparable" and its method compareTo.

So far, this is how I implemented the interface Comparable:

package modeloqytetet;

public interface Comparable {
    public int compareTo(Object otroJugador);
}

Inside class Player this is how I implemented the said method:

@Override
    public int compareTo(Object otherJugador) {
        int otherCapital = ((Player) otherJugador).getCapital();

        return otherCapital-getCapital();
    }

Now, the method obtainRanking() : void should be implemented in other class and I don't know how to do it. I've been trying to figure out by looking some examples around the internet but nothing seems to work.

Any help would be appreciated.

9
  • Any method called obtainRanking should not return void. Nevertheless, You can pass the the Comparator to sort method that you use in the method in other class. Commented Oct 21, 2018 at 11:34
  • 6
    Maybe unrelated but I think they mean that they want you to implement Comparable and not your own Comparable interface Commented Oct 21, 2018 at 11:34
  • @Sid I think that the purpose of the method is to sort an existing ArrayList of Player class so the return should be void. Anyway the naming is not right, but I didn't choose it :P Commented Oct 21, 2018 at 11:35
  • So you can directly sort in obtainRanking method then, can't you? Commented Oct 21, 2018 at 11:36
  • 1
    Hi d3vcho! You'll be needing this method: docs.oracle.com/javase/8/docs/api/java/util/… - put your player instances into a collection, use that to sort it, then the ranking you want is their position in the sorted list. Hope that helps! Commented Oct 21, 2018 at 11:37

2 Answers 2

3

The instructions is telling you to implement java.lang.Comparable<T>, not your own Comparable interface.

You should do this:

class Player implements Comparable<Player> {
    @Override
    public int compareTo(Player other) {
        return Integer.compare(this.getCapital(), other.getCapital());
    }

    ...
}

For why you should not simply subtract one integer from another to compare them, see here.

Then, you can implement obtainRankings like this:

// this name is quite bad. I would call it sortPlayersCapitalInPlace or something like that
public void obtainRankings(List<Player> players) {
    Collections.sort(players);
}
Sign up to request clarification or add additional context in comments.

4 Comments

That seems good! One more thing, shouldn't I @Override the method compareTo?
@d3vcho The @Override annotation is optional. I didn't write it there because I was lazy... :). You should write it.
@d3vcho it's good practice, yes - but it's not actually required. In fact it was only in Java 7 that it was legal to annotate implemented interface methods with @Override.
@d3vcho You are overriding it, but if you are asking if you should add @Override annotation than yes, it should be used whenever your intention is to override some method to make compiler test that fact. More at When do you use Java's @Override annotation and why?
0

You can use the following code snippets in your obtainRankings() method for customized sorting.

For Ascending order of players,

public static void obtainRankings(List<Player> list){
        Collections.sort(list, (pl1, pl2) -> {
            return ((pl1.getCapital() > pl2.getCapital()) ? 1 : -1);
        });
    }

For Descending order of players,

public static void obtainRankings(List<Player> list){
        Collections.sort(list, (pl1, pl2) -> {
            return ((pl1.getCapital() > pl2.getCapital()) ? -1 : 1);
        });
    }

Comments

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.