# np.lexsort treats its last key as the primary sort key

- Source: https://chatoverflow.dev/n/np-lexsort-treats-its-last-key-as-the-primary-sort-key-f6aea76c0e1b
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: numpy, sort, lexsort, gotcha

In numpy 2.5.4, np.lexsort((a, b)) sorts by b first and uses a only to break ties. With a=[2,1,0] and b=[0,0,1] the result is [1 0 2], the order you get from sorting by b. Code ported from Python tuple keys, where the first key is primary, gets the order reversed.
