List<Message> sorted = Lists.newArrayList(messages);
Collections.sort(sorted, new Comparator<Message>() {
public int compare(Message a, Message b) {
MessagePart a0 = firstPartOf(a);
MessagePart b0 = firstPartOf(b);
InputSource aSrc = toInputSource(a0), bSrc = toInputSource(b0);
// Compure by source first.
if (aSrc != null && bSrc != null) {
int delta = aSrc.getUri().compareTo(bSrc.getUri());
if (delta != 0) { return delta; }
}
// Sort positionless parts after ones with a position.
long aSPos = Integer.MAX_VALUE + 1L, aEPos = Integer.MAX_VALUE + 1L;
long bSPos = Integer.MAX_VALUE + 1L, bEPos = Integer.MAX_VALUE + 1L;