on sorting things

Peter Otten __peter__ at web.de
Fri Dec 20 13:59:53 EST 2019


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.

> There ARE good reasons to do weird things with sorting, and a custom
> key object (either with cmp_to_key or directly implemented) can do
> that.

Indeed.



More information about the Python-list mailing list