No, the pairs are not sorted by their second element. Instead the same order as in `ls` is preserved. Rearranging the global indices for same component is wrong, that's why there is a `stable_sort` call instead of `sort`.