Skip to main content

ArrayList Sorting :- Part 1 – using Comparable and Comparator

In this tutorial, we’ve shared few examples of sorting ArrayList.

String ArrayList

Collections.sort() and Collections.reverseOrder() can be used for sorting and reverse sorting.

output :-

 

Sorting an Object ArrayList using Comparable

we have to implement Comparable interface and override compareTo().

Output :-

 

Using Comparator

Using Comparable, we can only sort single property. What will you do if you want to sort User’s name or other property? Then we have to user Comparator.

Output :-

Part 2 will be how to sort using Java 8 APIs.


Related Post

26657total visits,6visits today

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/arraylist-sorting-part-1-using-comparable-comparator
Twitter