A comment that sorting on a pair sorts on the first member of the pair might be useful?  Does this guarantee that items that have the same index are in the correct order - if not, they won't be nested.