Search

Friday, October 30, 2015

How to Sort List into Ascending and Descending Order in Java

ArrayList, Set Sorting in Ascending – Descending Order Java
Sorting List, Set and ArrayList in Java on ascending and descending order is very easy, You just need to know correct API method to do that. Collections.sort()  method will sort the collection passed to it,  doesn't return anything just sort the collection itself.  Sort() method of Collections class in Java is overloaded where another version takes a Comparator and sort all the elements of Collection on order defined by Comparator.If we  don't pass any Comparator than object will be sorted based upon there natural orderlike String will be sorted alphabetically or lexicographically. Integer will be sorted numerically etc. Default sorting order for an object is ascending order like Integer will be sorted  from low to high while descending order is just opposite. Collections.reverseOrder() returns a Comparator which will be used for sorting Object in descending order.
Read more »

No comments:

Post a Comment