Improved sort to handle "tuples" (or "pairs") of values