-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?

3
  • 1
    Ref. stackoverflow.com/questions/2784514/… (it's for a List, then see Arrays.sort) Commented Mar 27, 2014 at 17:26
  • 1
    Read about Arrays.sort and Comparator Commented Mar 27, 2014 at 17:27
  • 1
    Turn your eyes on the right, to the "Related" section, and you'll find dozens of duplicates of your question. Commented Mar 27, 2014 at 17:30

5 Answers 5

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));
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

Note that this might not work for large negative values of number. Better use Integer.compare
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.

Comments

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);

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.