Using Java Arrays.sort() to sort arrays with a custom comparator

I was asked a question about how to sort Java arrays using custom comparators today. This is how. Being late, I’ll skip the lengthy explanation for now…Here’s the code example:

Download ComparatorExample Source Files

Program.java:

import java.util.Arrays;

public class Program {
public static void main(String[] args) {
// Defining three teams with three players each, all out of order
Team [] teams = new Team[] {
new Team(“T2”, new Player[]{
new Player(“P2”, “A”),
new Player(“P3”, “B”),
new Player(“P1”, “C”)
}),
new Team(“T1”, new Player[]{
new Player(“P2”, “B”),
new Player(“P3”, “A”),
new Player(“P1”, “C”)
}),
new Team(“T3”, new Player[]{
new Player(“P2”, “C”),
new Player(“P3”, “A”),
new Player(“P1”, “B”)
})
};

int i = 0, j = 0;
// For storing a copy of each team’s player array to sort
Player [] tmp;

// Sort the teams by name
Arrays.sort(teams, new TeamSort());

// Sort each team’s player
for(i=0; i<teams.length; i++) {
System.out.println(“Team: ” + teams[i].toString());

tmp = teams[i].getPlayers();

// Sort by first name and display
System.out.println(“Players By First Name: “);
Arrays.sort(tmp, new PlayerSort());
for(j = 0; j < tmp.length; j++) {
System.out.println(” ” + tmp[j].toString());
}

// j = 0; // Debug breakpoint, players are sorted by first name

// Sort by last name and display
System.out.println(“Players By Last Name: “);
Arrays.sort(tmp, new PlayerLastNameSort());
for(j = 0; j < tmp.length; j++) {
System.out.println(” ” + tmp[j].toString());
}
}

// i = 0; // Debug breakpoint, players are sorted by last name
}
}

Player.java:

public class Player {
public Player() {}
public Player(String name) { setName(name); }
public Player(String name, String lastName) { setName(name); setLastName(lastName); }

private String Name;
public String getName() { return Name; }
public void setName(String name) { Name = name; }

private String LastName;
public String getLastName() { return LastName; }
public void setLastName(String name) { LastName = name; }

public String toString() { return Name + ” ” + LastName; }
}

PlayerSort.cs:

import java.util.Comparator;

public class PlayerSort implements Comparator<Player> {
public int compare(Player o1, Player o2){

// Sort the players by first name
return o1.getName().compareTo(o2.getName());
}
}

PlayerLastNameSort.java:

import java.util.Comparator;

public class PlayerLastNameSort implements Comparator<Player> {
public int compare(Player o1, Player o2){

// Sort the players by last name
return o1.getLastName().compareTo(o2.getLastName());
}
}

Team.java:

public class Team {
public Team() {}
public Team(Player[] players) { setPlayers(players); }
public Team(String name, Player[] players) { setName(name); setPlayers(players); }

private String Name;
public String getName() { return Name; }
public void setName(String name) { Name = name; }

private Player[] Players;
public Player[] getPlayers() { return Players; }
public void setPlayers(Player[] players) { Players = players; }

public String toString() { return Name; }
}

TeamSort.java:

import java.util.Comparator;

public class TeamSort implements Comparator<Team> {
public int compare(Team o1, Team o2){

// Sort the teams by name
return o1.getName().compareTo(o2.getName());
}
}

Like this article? Feed the developer, every dollar counts: