[issue35010] sort by partially reversed key tuple

Steven D'Aprano report at bugs.python.org
Wed Oct 17 06:34:16 EDT 2018


Steven D'Aprano <steve+python at pearwood.info> added the comment:

Since sort is guaranteed to be stable, can't you sort in two runs?


py> values = ['bc', 'da', 'ba', 'abx', 'ac', 'ce', 'dc', 'ca', 'aby']
py> values.sort(key=itemgetter(1), reverse=True)
py> values.sort(key=itemgetter(0))
py> values
['ac', 'abx', 'aby', 'bc', 'ba', 'ce', 'ca', 'dc', 'da']


Its not as efficient as doing a single sort, but its easier to understand than a complex API to specify each item's sort direction individually, and therefore probably less likely to mess it up and get it wrong.

----------
nosy: +steven.daprano, tim.peters

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35010>
_______________________________________


More information about the Python-bugs-list mailing list