Incorrect refactor when removing nested `sorted`
Zac-HD opened this issue · comments
Zac Hatfield-Dodds commented
>>> sorted(sorted([1, 12, 11]), key=lambda x: str(x)[0])
[1, 11, 12]
>>> sorted([1, 12, 11], key=lambda x: str(x)[0])
[1, 12, 11]
Replacing the latter with the former is clearly a mistake, because the stability of sorting is important! We should therefore avoid removing an inner sorted
when either has a key=
argument.
Fancier solutions involving returning a tuple from the key argument are out of scope, since covering all the scope-related edge cases really isn't worth the code it'd take.