top-like sorting, make sort stable (no shifting rows)

Pretty straightforward; I've sent the same patches to Guillaume.