When a dashboard needs only a few leaders, a top-k operation avoids constructing a complete sorted report. Keep the desired output order explicit.
Worked example
import heapq
print(heapq.nlargest(3, [4, 9, 2, 7, 5]))Result
[9, 7, 5]Implementation notes
A ranking key and a tie-breaker serve different purposes. Include a stable secondary key if the same report must select exactly the same three entries.
Try it yourself
Request more items than the input contains and inspect ties.
Open the Python example →Editorial date May 1, 2026 · Published December 6, 2026