Pages

Showing posts with label Collections. Show all posts
Showing posts with label Collections. Show all posts

Monday, January 10, 2011

Sorting the objects using collections

We can implement using two following ways:

Way 1:
Step 1:
the object which supposed to sort in collection needs to be implements Comparable interface
For Ex: Object TestPOJO needs to be sort so that class looks like below:

Tuesday, December 21, 2010

Sorting the the List of Objects using Collections


A List l may be sorted as follows.
Collections.sort(l);
If the List consists of String elements, it will be sorted into alphabetical order. If it consists of Date elements, it will be sorted into chronological order. How does this happen? String and Date both implement the Comparable interface. Comparable implementations provide a natural ordering for a class, which allows objects of that class to be sorted automatically.