-1

I have an object array like

Player p[] = new Player[t];

And every object has an integer variable and a name variable.

public class Player {
   public String player_name;
   public int number;
}

After getting input from user how can I sort this object array according to their number variable?

Tiny
  • 27,221
  • 105
  • 339
  • 599
TheHost
  • 45
  • 2
  • 9
  • 1
    Ref. http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property?rq=1 (it's for a List, then see [Arrays.sort](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator))) – user2864740 Mar 27 '14 at 17:26
  • 1
    Read about `Arrays.sort` and `Comparator` – Edwin Dalorzo Mar 27 '14 at 17:27
  • 1
    Turn your eyes on the right, to the "Related" section, and you'll find dozens of duplicates of your question. – JB Nizet Mar 27 '14 at 17:30

5 Answers5

1

With Java SE 8 try something like (untested):

Arrays.stream(p).sorted((p1, p2) -> Integer.compare(p1.number, p2.number)) .collect(Collectors.toList())

Edit

On second thought this might be more efficient in this case, as it doesn't create a new array/ collection:

Arrays.sort(p, (p1, p2) -> Integer.compare(p1.number, p2.number));
Puce
  • 37,247
  • 13
  • 80
  • 152
0

Create a class that implements Comparator<Player>, called say PlayerComparator. Implement the interface by creating a compare method that takes two Players. Return a negative number, zero, or a positive number, depending on whether the first Player is considered less than, equal to, or greater than, the second Player.

Then you can create your PlayerComparator and pass it (along with your array) to Arrays.sort.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

let your custom class implements Comparable interface and then you can use java Collection to sort your array (Arrays.sort() function)

public class Player implements Comparable<Player> {
   public String player_name;
   public int number;

    @Override
    public int compareTo(Player object) {
        return this.player_name.compareTo(object.player_name);
    }
}

sort

Player p[] = new Player[t];
// do stuff
Arrays.sort(p);
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
0

The easiest way is to make Player implement Comparable<Player>, and implement the compareTo(Player) method inside Player:

public int compareTo(Player player) {
  return this.number-player.number;
}

Then, wherever you want to sort the array, just call Arrays.sort(p);

aryn.galadar
  • 716
  • 4
  • 13
  • Note that this might not work for large negative values of number. Better use `Integer.compare` – Puce Mar 27 '14 at 17:45
0

You'd need to have a getter on the number variable for this to work, like:

public class Player {
   public String player_name;
   public int number;

   public int getNumber() {
       return number;
   }
}

Then, as of Java 8, you can sort it by using the Comparing factory class:

Player[] p = new Player[t];
Arrays.sort(p, Comparator.comparingInt(Player::getNumber));

This will:

  • Create a Comparator<Person>, by comparing no the getNumber method.
  • What you see here is a method reference, which is shorthand for the lambda p -> player.getNumber(), which maps the player to the number.
skiwi
  • 66,971
  • 31
  • 131
  • 216