I have some lists that contain elements of type DataTime( Joda-time ). How can I sort them by date? It will be great if somebody gave link to the example...
            Asked
            
        
        
            Active
            
        
            Viewed 1,173 times
        
    -2
            
            
        - 
                    Look into `Collections.sort` and `Comparator`. – nkr Oct 10 '12 at 21:05
- 
                    @nkr I think you mean Comparator – Jordan Kaye Oct 10 '12 at 21:06
- 
                    Hint: sort by timestamp. – jn1kk Oct 10 '12 at 21:06
- 
                    Can you show the outline of the code? – Stas0n Oct 10 '12 at 21:07
2 Answers
9
            Because the objects of your list implement the Comparable interface, you can use
Collections.sort(list);
where list is your ArrayList. 
Relevant Javadocs:
Edit: If you want to sort a list of a custom class that contains a DateTime field in a similar way, you would have to implement the Comparable interface yourself. For example,
public class Profile implements Comparable<Profile> { 
    DateTime date;
    double age; 
    int id; 
    ...
    @Override
    public int compareTo(Profile other) {
        return date.compareTo(other.getDate());  // compare by date
    }
}
Now, if you had a List of Profile instances, you could employ the same method as above, namely Collections.sort(list) where list is the list of Profiles.
 
    
    
        arshajii
        
- 127,459
- 24
- 238
- 287
- 
                    1And what if the ArrayList contains objects of class Profile public Profile{ DateTime date; Double age; int id; } How can i sort it by date? – Stas0n Oct 10 '12 at 21:13
- 
                    1
6
            
            
        DateTime already implements Comparable you just need to use Collections.sort()
- 
                    1And what if the ArrayList contains objects of class Profile public Profile{ DateTime date; Double age; int id; } How can i sort it by date? – Stas0n Oct 10 '12 at 21:16
- 
                    1
- 
                    1
- 
                    1
- 
                    1http://stackoverflow.com/questions/5178092/sorting-a-list-of-points-with-java – jmj Oct 10 '12 at 21:21
 
     
    