1

I am fairly new to Java (C++ and C are my usual languages)

I am trying to figure out how to sort an array of objects. I need to have users enter in how many players there are, Players names, and players scores. The program will then output the Scores and players names from Top to Bottom.

I have the users entering in the their info, and am storing it to a Player Class.

I cannot figure out how to sort the Players objects scores. I'm pretty sure I need to use a comparable, but for the life of me I cant figure out how to set it up.

Can anyone help?

I know the code in the player class isn't correct lol

import java.util.*;
import java.util.Arrays;

public class HelloWorld {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        Scanner input1 = new Scanner(System.in);
        int allPlayers;
        int index[] = new int[12];
        int i= 0;
        System.out.print("Please enter the number of players");
        allPlayers = input.nextInt();

        Player[]  playersArray = new Player[allPlayers];

        for(i = 0; i <allPlayers; i++){
            playersArray[i] = new Player();
            System.out.print("What is the name of Player # " + (i+1) +"?");
            playersArray[i].name = input1.nextLine();
          System.out.print("What was the score of Player # " + (i+1) + "?");
            playersArray[i].score = input.nextInt();
        }

              System.out.print(playersArray[i].name);
              System.out.print(playersArray[i].score);
    }
}
public class Player implements Comparable<Player> {
    private int score;    // players score 
    private String name;  // players name

    public Player(int score, String name){
        this.core = score;
        this.name = name;
    }
    public int compareTo(Player other){
        int last = this.score.compareTo(other.score);
        return last == 0 ? this.name.compareTo(other.score) : score;
    }
}
  • 1
    Take a look at [Arrays.sort](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])) – rpax Nov 23 '15 at 00:49
  • Possible duplicate of [How to use Java comparator properly?](http://stackoverflow.com/questions/7117044/how-to-use-java-comparator-properly) – Enamul Hassan Nov 23 '15 at 01:06

2 Answers2

1

The problem in your code is a misunderstanding of the compareTo() method.

The method returns -1 if the first argument (in this case, this is considered the first argument) is greater, 0 if they are equal, and 1 if the second argument is greater.

public int compareTo(Player other){
    if (other == null){
        return -1; // If the other is null, it goes last
    }

    // First, compare by the score:
    int scoreComparison = Integer.compare(this.score, other.score);

    // That's good enough, unless the scores are equal
    if (scoreComparison != 0){ // then they're not equal, so we know our answer
        return scoreComparison;
    } else { // The scores are equal, so compare the names and return the result
        if (this.name == null){ // Equal if both null, otherwise non-null wins
            return other.name == null ? 0 : 1;
        } else {
            return this.name.compareTo(other.name);
        }
    }
}
Steve K
  • 4,863
  • 2
  • 32
  • 41
0

Cake walk. Did some simplification and cleaning up.

class Player implements Comparable<Player> {
    public final int    score;  // players score
    public final String name;   // players name

    public Player(final int score, final String name) {
        this.score = score;
        this.name = name;
    }
    @Override public int compareTo(final Player other) {
        return other.score - this.score;
        // return this.score - other.score; // or this to reverse order
    }
}

public class PlayerSorting {

    public static void main(final String[] args) {
        try (final Scanner input = new Scanner(System.in);//
                final Scanner input1 = new Scanner(System.in);) {
            System.out.print("Please enter the number of players");
            final int allPlayers = input.nextInt();

            final Player[] playersArray = new Player[allPlayers];
            for (int i = 0; i < allPlayers; i++) {
                System.out.print("What is the name of Player # " + (i + 1) + "?");
                final String name = input1.nextLine();
                System.out.print("What was the score of Player # " + (i + 1) + "?");
                final int score = input.nextInt();
                playersArray[i] = new Player(score, name);
            }

            // sort
            Arrays.sort(playersArray);

            // output all
            System.out.println("SCORES:");
            for (final Player player : playersArray) {
                System.out.println("\t" + player.name + "\t" + player.score);
            }
        }
    }

}
JayC667
  • 2,418
  • 2
  • 17
  • 31