on sorting things

Chris Angelico rosuav at gmail.com
Fri Dec 20 14:44:03 EST 2019


On Sat, Dec 21, 2019 at 6:01 AM Peter Otten <__peter__ at web.de> wrote:
>
> Chris Angelico wrote:
>
> > On Sat, Dec 21, 2019 at 5:03 AM Peter Otten <__peter__ at web.de> wrote:
> >> PS: If you are sorting files by size and checksum as part of a
> >> deduplication effort consider using dict-s instead:
> >
> > Yeah, I'd agree if that's the purpose. But let's say the point is to
> > have a guaranteed-stable ordering of files that are primarily to be
> > sorted by file size - in order to ensure that two files are in the
> > same order every time you refresh the view, they get sorted by their
> > checksums.
>
> One thing that struck me about Eli's example is that it features two key
> functions rather than a complex comparison.
>
> If sort() would accept a sequence of key functions each function could be
> used to sort slices that compare equal when using the previous key.
>

That would basically make it a comparison function, not a key function
:) The value of the key function is that it's called exactly once per
element, and the result is what you sort by. There's no correlation to
any other element. It's effectively this:

# sortme.sort(key=keyfunc)
keys = sortme.map(keyfunc)
keys.sort(keep_in_parallel=sortme)

Which is why cmp_to_key is the correct solution to that problem.

(For cases where you don't mind pre-calling both key functions, of
course, it's equivalent to a single key function that returns a
tuple.)

ChrisA


More information about the Python-list mailing list