How to Sort ArrayList using Comparator?

15 Dec, 2020
Comparator is an interface that is used for rearranging the Arraylist in a sorted manner. Comparator is used to sort an ArrayList of User-defined objects. In java, Comparator is provided in java.util package. Using Comparator we can sort ArrayList on the basis of multiple variables. 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.
Syntax:
Collections.sort(list, comparator)
Parameters:
list: List which should be sorted based on the comparator.
comparator: Comparator class instance
Returns: It sorts the list and does not return anything.
Example
Java
// Java program to Sort ArrayList using Comparator  import java.util.*;  // create the Shop classclass Shop {    int ProductNo;    String name;    int stock;      // constructor    Shop(int ProductNo, String name, int stock)    {        this.ProductNo = ProductNo;        this.name = name;        this.stock = stock;    }}  // creates the comparator for comparing stock valueclass StockComparator implements Comparator<Shop> {      // override the compare() method    public int compare(Shop s1, Shop s2)    {        if (s1.stock == s2.stock)            return 0;        else if (s1.stock > s2.stock)            return 1;        else            return -1;    }}  class GFG {    public static void main(String[] args)    {        // create the ArrayList object        ArrayList<Shop> s = new ArrayList<Shop>();        s.add(new Shop(218, "Pen", 520));        s.add(new Shop(223, "Pencil", 213));        s.add(new Shop(423, "Books", 101));        s.add(new Shop(512, "Toy", 59));        s.add(new Shop(723, "Bottle", 10));          System.out.println("before sorting");        for (Shop shop : s) {            System.out.println(shop.stock + " " + shop.name                               + " " + shop.ProductNo);        }        System.out.println();          System.out.println(            "After sorting(sorted by Stock)");          // call the sort function        Collections.sort(s, new StockComparator());        for (Shop shop : s) {            System.out.println(shop.stock + " " + shop.name                               + " " + shop.ProductNo);        }    }}
before sorting
520 Pen 218
213 Pencil 223
101 Books 423
59 Toy 512
10 Bottle 723

After sorting(sorted by Stock)
10 Bottle 723
59 Toy 512
101 Books 423
213 Pencil 223
520 Pen 218

In the above example, we sort the Shop class by the number of stock available. We can also sort it on the basis of name and ProductNo. Let’s sort the above ArrayList based on the name.
Example 2
Java
// Java program to Sort ArrayList using Comparator  import java.util.*;  // create the Shop classclass Shop {      int ProductNo;    String name;    int stock;      // constructor    Shop(int ProductNo, String name, int stock)    {        this.ProductNo = ProductNo;        this.name = name;        this.stock = stock;    }}  // creates the comparator for comparing nameclass NameComparator implements Comparator<Shop> {      // override the compare() method    public int compare(Shop s1, Shop s2)    {        return s1.name.compareTo(s2.name);    }}  class GFG {    public static void main(String[] args)    {        // create the ArrayList object        ArrayList<Shop> s = new ArrayList<Shop>();        s.add(new Shop(218, "Pen", 520));        s.add(new Shop(223, "Pencil", 213));        s.add(new Shop(423, "Books", 101));        s.add(new Shop(512, "Toy", 59));        s.add(new Shop(723, "Bottle", 10));          System.out.println("before sorting");        for (Shop shop : s) {            System.out.println(shop.name + " " + shop.stock                               + " " + shop.ProductNo);        }        System.out.println();          System.out.println("After sorting(sorted by Name)");          // call the sort function        Collections.sort(s, new NameComparator());        for (Shop shop : s) {            System.out.println(shop.name + " " + shop.stock                               + " " + shop.ProductNo);        }    }}
before sorting
Pen 520 218
Pencil 213 223
Books 101 423
Toy 59 512
Bottle 10 723

After sorting(sorted by Name)
Books 101 423
Bottle 10 723
Pen 520 218
Pencil 213 223
Toy 59 512

Java-ArrayList
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.