Sort with a key
transcript
Sort words by length. Then by text. In four languages.
Python uses a key pair. Java uses a comparator. Sort a copy of the list. Test with equal keys. Equal keys are the test. Compare the outputs. Fix them in the code.
C plus plus uses a comparator. So does JavaScript. Keep the caller's list as it was. Small lists hide the problem. Add the tie break early. They must match exactly. Then they stay fixed.
The default JavaScript sort compares text. Ten, nine, one comes out wrong. Pass a comparator. Write the tie break yourself. Large lists show it. Then the order is certain. If not, find the tie. In every language.
Python's sort is stable. Java's object sorts are stable. C plus plus std sort is not. Never trust the default order. Compare both orders by hand. Run it in each language. Ties hide in sorts and maps. That is the toolkit.
Tie breaks belong in your code. Full breakdown in the lecture.