Comparators

Just like society

Sorting an Object array : In order to sort an object array, all elements must implement either Comparable or Comparator interface to define the order of the sorting.

Consider we have a task of sorting objects that are instances of the following class:

public class User {
    public final Long id;
    public final String username;

    public User(Long id, String username) {
        this.id = id;
        this.username = username;
    }

    @Override
    public String toString() {
        return String.format("%s:%d", username, id);
    }
}

In order to use Collections.sort(List list) we need to modify the User class to implement the Comparable interface. For example

public class User implements Comparable<User> {
    public final Long id;
    public final String username;

    public User(Long id, String username) {
        this.id = id;
        this.username = username;
    }

    @Override
    public String toString() {
        return String.format("%s:%d", username, id);
    }

    @Override
    /** The natural ordering for 'User' objects is by the 'id' field. */
    public int compareTo(User o) {
        return id.compareTo(o.id);
    }
}

Consider the following to use this implementation

You can also compare the username

Last updated

Was this helpful?