0

I have an ArrayList of Player(s). The array is currently ordered in the player who've been logged on first, to the most recent one.

However, i want to sort the ArrayList from the Players positions, high to low. The position is stored in the Player class, retrieved by

player.getX();

I imagine i should have a loop with each item being added to a new ArrayList, but i can't quite figure out how to actually implement it.

How can i do this?

Loop:

for (int i = 0; i < otherPlayers.size(); i++) {
    Player currentPlayer = otherPlayers.get(i);
}

Edit: Added loop example

Patrick Reck
  • 303
  • 1
  • 10
  • 24

5 Answers5

5

Either you make Player implementing Comparable and use Collections.sort(players) or you can directly create a comparator like this

    Collections.sort(players, new Comparator<Player>() {
        public int compare(Player o1, Player o2) {
            return o2.getX().compareTo(o1.getX());
        }
    });

Assuming player.getX() returns a Integer

Edit

Let's say you have your List<Player> named otherPlayers if you want to sort it directly without writing yourself a loop and the algorithm of sorting, you can rely on the Java built-in method Collections.sort() to sort it. But this means that either your class Person implements the Comparable interface, or you can pass an instance of Comparator as the second argument of the Collections.sort() method as done above.

The Comparator.compare() method takes two object and does return an integer that says if the first argument is lower than the second, equals or greater. This is how the sorting algorithm will know how to order the elements in your list. So, by creating a Comparator instance that compare the result of Player.getX() of two Player instances (the elements of your list), you are able to sort the original list on the position criteria as you asked for it.

Alex
  • 25,147
  • 6
  • 59
  • 55
  • +1 for Comparator, it's less invasive than Comparable, Players can be ordered by more factors than position – Kojotak Dec 04 '12 at 10:01
  • I'm very new at Java, and i can't figure out how to implement that. I've added a run-through loop of the actual code. Could you perhaps extend your answer? Thank you! – Patrick Reck Dec 04 '12 at 10:06
  • Thank you! However, i'm getting "int cannot be dereferenced" when implementing it. I suppose "compare(Player o1, Player o2)" the o1 and o2 players doesn't have any coordinates? – Patrick Reck Dec 04 '12 at 10:48
1

Sort it with Collections#sort.

List<Player> players = new ArrayList<Player>();
Collections.sort(players, new Comparator<Player>(){
                    public int compare(Player p1, Player p2){
                      return p1.getX().compareTo(p2.getX());
                    }
                 });
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • This assumes there's just 2 players, however there is an unknown amount of players. How would i implement that in your example? :-) – Patrick Reck Dec 04 '12 at 10:13
  • nope,it'll sort all players of your list. you can check this source [code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collections.java#Collections.sort%28java.util.List%2Cjava.util.Comparator%29) – Subhrajyoti Majumder Dec 04 '12 at 10:17
  • I'm getting "int cannot be dereferenced" using this! – Patrick Reck Dec 04 '12 at 11:01
0

Player should implement Comparable<Player>

Then you have to implement a compareTo method in Player class. After that sort using Collections.sort(listOfPlayers);

http://www.vogella.com/blog/2009/08/04/collections-sort-java/

jlordo
  • 37,490
  • 6
  • 58
  • 83
Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
0

let Player implement

Comparable

Then implement

compareTo() 

method and make comparison by positions. Then you can make sort by means of

Collections.sort(yourList);

or

Collections.sort(yourList, Collections.reverseOrder());
Romczyk
  • 361
  • 3
  • 12
0

Try Collections.sort(List, Comparator)

ironwood
  • 8,936
  • 15
  • 65
  • 114