Sort ArrayList objects by name and name in Java

I have a list of elements of different types of players based on sports. I need to sort the list of players in the arrayList list by name to get started. If 2 players have the same name, you must sort these 2 players by name. Example: Format First Name Last Name Williams Robert Phillips Warren Dow John Phillips Mark

The way out should be Dow John Phillips Mark Phillips Warren Williams Robert

Now I only have sorting by first or last, I have the last atm in my code.

public static void sortPlayers(ArrayList playerList) { for (int i = 0; i < playerList.size(); i++) { for (int j = 0; j < playerList.size(); j++) { Collections.sort(playerList, new Comparator() { public int compare(Object o1, Object o2) { PlayerStats p1 = (PlayerStats) o1; PlayerStats p2 = (PlayerStats) o2; return p1.getPlayerLastName().compareToIgnoreCase(p2.getPlayerLastName()); } }); } } } 
+5
source share
4 answers

Change the comparator to:

  public int compare(Object o1, Object o2) { PlayerStats p1 = (PlayerStats) o1; PlayerStats p2 = (PlayerStats) o2; int res = p1.getPlayerLastName().compareToIgnoreCase(p2.getPlayerLastName()); if (res != 0) return res; return p1.getPlayerFirstName().compareToIgnoreCase(p2.getPlayerFirstName()) } 
+15
source

Petar's answer is correct, only two remarks:

  • Use List instead of ArrayList as an argument to the method, as the interface is more general and this method will work even if you switch to another type of List (e.g. LinkedList ...) later
  • Use generators to make the code more secure.

Enhanced Version:

 //the place where you define the List List<PlayerStats> playerList = new ArrayList<PlayerStats>(); public static void sortPlayers(List<PlayerStats> playerList) { Collections.sort(playerList, new Comparator<PlayerStats>() { public int compare(PlayerStats p1, PlayerStats p2) { int res = p1.getPlayerLastName().compareToIgnoreCase(p2.getPlayerLastName()); if (res != 0) return res; return p1.getPlayerFirstName().compareToIgnoreCase(p2.getPlayerFirstName()) } }); } 
+4
source

Using java8, there is an easy way to do this:

 public List<PlayerStats> getSortedPlayerList(List<PlayerStats> playerList) { return playerList.stream().sorted(Comparator.comparing(PlayerStats::getPlayerLastName).thenComparing(PlayerStats::getPlayerFirstName)).collect(Collectors.toList()); } 
+2
source
  //requires java@8 //class Person { String fName; String lName; int id} List<Person> list = new ArrayList<>(); Person p1 = new Person(); p1.setfName("a"); p1.setlName("x"); list.add(p1 ); Person p4 = new Person(); p4.setfName("b"); p4.setlName("z"); list.add(p4); Person p3 = new Person(); p3.setfName("a"); p3.setlName("z"); list.add(p3); Person p2 = new Person(); p2.setfName("a"); p2.setlName("y"); list.add(p2); //sort by a single field Collections.sort(list, (o1,o2) -> o1.getfName().compareTo(o2.getfName())); //sort by multiple cascading comparator. Collections.sort(list, Comparator.comparing(Person::getfName).thenComparing(Person::getlName)); list.forEach( System.out::println); //output //Person [fName=a, lName=x, id=null] //Person [fName=a, lName=y, id=null] //Person [fName=a, lName=z, id=null] //Person [fName=b, lName=z, id=null] 
0
source

All Articles