Fix stableSort and add tests