You can implement your own Comparator
without extending the class, and use it as a parameter for Collections.sort
:
public class MyComparator implements Comparator<MyClass> {
@Override
int compate (MyClass a, MyClass b) {
// null handling removed for clarity's sake
// add it if you need it
return a.getName().compareTo(b.getName());
}
}
And now just use it:
List<MyClass> myList = ...;
Collections.sort (myList, new MyComparator());