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:
In order to use Collections.sort(List list)
we need to modify the User class to implement the Comparable interface. For example
Consider the following to use this implementation
You can also compare the username
Last updated