Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an arraylist of objects by a property?

Lets say you have an Arraylist of HockeyPlayer objects.

How could you sort that if they all have a variable int goalsScored. How could you sort them by goalsScored?

like image 203
sieve411 Avatar asked Mar 29 '10 00:03

sieve411


People also ask

How do you sort an array of objects based on a property in Java?

To sort an Object by its property, you have to make the Object implement the Comparable interface and override the compareTo() method. Lets see the new Fruit class again. The new Fruit class implemented the Comparable interface, and overrided the compareTo() method to compare its quantity property in ascending order.

How do you sort a list containing objects?

sort() method to sort a list of objects using some examples. By default, the sort() method sorts a given list into ascending order (or natural order). We can use Collections. reverseOrder() method, which returns a Comparator, for reverse sorting.

How do you sort elements in an ArrayList using comparable interface?

We can simply implement Comparator without affecting the original User-defined class. To sort an ArrayList using Comparator we need to override the compare() method provided by comparator interface. After rewriting the compare() method we need to call collections. sort() method like below.


2 Answers

You can use Collections.sort with a custom Comparator<HockeyPlayer>.

    class HockeyPlayer {         public final int goalsScored;         // ...     };      List<HockeyPlayer> players = // ...      Collections.sort(players, new Comparator<HockeyPlayer>() {         @Override public int compare(HockeyPlayer p1, HockeyPlayer p2) {             return p1.goalsScored - p2.goalsScored; // Ascending         }      }); 

The comparision part can also be written this way :

players.sort(Comparator.comparingInt(HockeyPLayer::goalsScored)); 

Alternatively, you can make HockeyPlayer implementsComparable<HockeyPlayer>. This defines the natural ordering for all HockeyPlayer objects. Using a Comparator is more flexible in that different implementations can order by name, age, etc.

See also

  • Java: What is the difference between implementing Comparable and Comparator?

For completeness, I should caution that the return o1.f - o2.f comparison-by-subtraction shortcut must be used with extreme caution due to possible overflows (read: Effective Java 2nd Edition: Item 12: Consider implementing Comparable). Presumably hockey isn't a sport where a player can score goals in the amount that would cause problems =)

See also

  • Java Integer: what is faster comparison or subtraction?
like image 195
polygenelubricants Avatar answered Sep 25 '22 19:09

polygenelubricants


As @user6158055 suggets, it's one liner with Java 8, as follows:

Collections.sort(                 hockeyPlayerList,                 (player1, player2) -> player1.getGoalsScored()                         - player2.getGoalsScored()); 

Complete example to depict the same:

import java.util.ArrayList; import java.util.Collections; import java.util.List;  public class Main {      public static void main(String[] args) {         List<HockeyPlayer> hockeyPlayerList = new ArrayList<>();         hockeyPlayerList.add(new HockeyPlayer("A", 3));         hockeyPlayerList.add(new HockeyPlayer("D", 10));         hockeyPlayerList.add(new HockeyPlayer("B", 2));          System.out.println("Before Sort based on goalsScored\n");          hockeyPlayerList.forEach(System.out::println);          System.out.println("\nAfter Sort based on goalsScored\n");          Collections.sort(                 hockeyPlayerList,                 (player1, player2) -> player1.getGoalsScored()                         - player2.getGoalsScored());          hockeyPlayerList.forEach(System.out::println);     }      static class HockeyPlayer {          private String name;         private int goalsScored;          public HockeyPlayer(final String name, final int goalsScored) {             this.name = name;             this.goalsScored = goalsScored;         }          public String getName() {             return name;         }          public void setName(String name) {             this.name = name;         }          public int getGoalsScored() {             return goalsScored;         }          public void setGoalsScored(int goalsScored) {             this.goalsScored = goalsScored;         }          @Override         public String toString() {             return "HockeyPlayer [name=" + name + ", goalsScored="                     + goalsScored + "]";         }      } } 

Output:

Before Sort based on goalsScored  HockeyPlayer [name=A, goalsScored=3] HockeyPlayer [name=D, goalsScored=10] HockeyPlayer [name=B, goalsScored=2]  After Sort based on goalsScored  HockeyPlayer [name=B, goalsScored=2] HockeyPlayer [name=A, goalsScored=3] HockeyPlayer [name=D, goalsScored=10] 
like image 39
Arpit Aggarwal Avatar answered Sep 23 '22 19:09

Arpit Aggarwal