David Segall said:
I haven't looked at the code so I don't know if it qualifies but the
easy way to do this is to use a sorting algorithm that maintains the
existing order on equal keys. The user can then sort on multiple
columns by requesting a sort on each key column from least to most
significant.
I did this for an older project, and I vaguely remember that the basic
design idea was to have a comparator maintained a linked list of other
comparators. The class would delegate to the first element in its list, and
upon ties, go down the list.
<UntestedCode strictnessLevel="off the top of my head">
public class MultiComparator implements Compatator<T> {
private List<Compatator<? super T>> myDelegates = new
LinkedList<Compatator<? super T>>();
/*Put various methods to manipulate the "myDelegates" list here.*/
public int compareTo(T a, T b) {
for(Comparator<? super T> c : myDelegates) {
int returnValue = c.compareTo(a, b);
if (returnValue != 0) {
return returnValue;
}
}
return 0;
}
}
</UntestedCode>
- Oliver