EASILY REVERSING A COLLECTION SORT If you're sorting a Collection and you want to provide an option to sort it in reverse order, try using a ReverseComparator. When coding their first Comparators, Java beginners often add some form of flag to specify that the sort should be performed in reverse. In time, they discover Collections.reverse(List) and start to use this instead. However, a single line of implementation can help avoid both of these poor designs. package com.generationjava.compare; import java.util.Comparator; public class ReverseComparator implements Comparator { private Comparator comparator; public ReverseComparator(Comparator comparator) { this.comparator = comparator; } public int compare(Object o1, Object o2) { // invert the wrapped comparator's result return -1*comparator.compare(o1,o2); } } This is a simple but remarkably powerful implementation. For example, take a UrlComparator, which sorts java.net.URLs in a particular order, grouping ftp://www.generationjava.com/ with http://generationjava.com and not with all the other ftps. To sort using this, we would do: List listUrls = ....; // contains java.net.URL Collections.sort( listUrls, new UrlComparator() ); To reverse this list, we would do: List listUrls = ....; // contains java.net.URL Collections.sort( listUrls, new ReverseComparator( new UrlComparator() ) ); Using this snippet of code can help eradicate constructors like: public SomeComparator(boolean reverse) { ------------------------------------------