Comparing Items or Computing Sort Keys

A comparator directly decides the order of a pair. A key function maps a single item to a value whose comparison determines that order.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Trace repeated use
  2. Preserve consistency

Trace repeated use

A comparator reading the date from two records may extract dates repeatedly. A key-based sort can compute each date key once in implementations that cache keys. See sorting with a key.

Preserve consistency

Changing the key during a sort makes pair choices hard to reason about and can violate the relation the algorithm needs. Choose a stable observation and check tie behavior.[1]

References

  1. Python Software Foundation, Functional Programming HOWTO ↩