0

I have an array of objects, these objects all have multiple values stored within them. I have them within an object array.

class PlayerScores {

String playerName;
   int pos=0;
   int played=0;
   int win=0;
   int draw=0;
   int lose=0;
   int goalsFor=0;
   int goalsAgainst=0;
   int goalDifference=0;
   int points=0;

   public PlayerScores() {
   }
}

These are stored within the array:

Player Scores[] playersObjects = new PlayerScores[int];

            playersObjects[i] = new PlayerScores();

I want to search through 'playersObject[]' and order then in a new object array with the Player with the highest points first in the array and the rest in descending order. I am not sure how to run a sort on a single value within an object.

Any help would be massively appreciated,

Thanks.

2
  • possible duplicate of Sort ArrayList of custom Objects by property Commented Jan 10, 2013 at 0:40
  • @PeterO. this is slightly different since it's with an array instead of an ArrayList (i.e. use Arrays.sort instead of Collections.sort), though admittedly it's markedly similar and doubtless there's another question out there that covers the same material somewhere on SO... Commented Jan 10, 2013 at 1:05

2 Answers 2

6

You can use Arrays.Sort and provide a custom Comparator. Something like this should work:

public class PlayerScoresComparator implements Comparator<PlayerScores> {
    @Override
    public int compare(PlayerScores lhs, PlayerScores rhs) {
        return lhs.points - rhs.points;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick reply, (sorry for my being a noob) but I am still unsure how to do this. How do I make a custom Comparator, I have read through the API and an struggling to see how I would select only the int .points from my object and compare those with all the other ones.
I cannot find in the Comparator how to get a certain variable from the class anywhere.
@OliBlack Added an implementation of a Comparator based on your class.
1

As an alternative to what kabuko suggested, you could use an ArrayList of PlayerScore objects, while implementing Comparable interface and adding a compareTo(PlayerScore another) method to PlayerScore class, like this:

public class PlayerScores implements Comparable<PlayerScores> {
  [...]

public int compareTo(PlayerScore another) {
  //example of a method to calculate which ogject is "greater".
  //See Comparable documentation for details. You will need to implement proper logic
  return getHighScore() - another.getHighScore(); 
}

Then you will be able to sort the ArrayList by Collections.sort():

ArrayList<PlayerScores> scores = new ArrayList<PlayerScores>();
[...] //Populate scores list
Collections.sort(scores)

This will be useful if you're have to use an ArrayList anyways, i.e. if you're going to attach it to a ListView.

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.