Lexicographic Sorting: Key Priority and Stable Positions
2026-11-18

Lexicographic Sorting: Key Priority and Stable Positions

Lexicographic sorting combines several aligned keys into one ordering of records. NumPy lexsort returns integer positions, leaving the original arrays available for gathering. Its convention is especially important: the final key supplied is primary, and preceding keys resolve ties in reverse argument order.

The last supplied key makes the first comparison

The NumPy lexsort reference defines an indirect stable sort. Consider five fictional records with original positions zero through four. Their group keys are B, A, B, A and A. Their level keys are two, two, one, one and one. These are invented labels for an offline example, not equipment measurements or customer records.

To order first by group and then by level, pass the level array before the group array. Conceptually, the call is lexsort of level, group. Group is primary because it appears last. All A records precede B records. Within A, the level-one records precede the level-two record. Within B, level one precedes level two.

The resulting index order is three, four, one, two, zero. Reading both key arrays through this same index gives pairs A-one, A-one, A-two, B-one and B-two. Reversing the key arguments changes the question: level becomes primary and group becomes its tie-breaker. It is not merely another spelling of the same instruction.

GroupB,A,B,A,A andlevel2,2,1,1,1yieldindex3,4,1,2,0withgroupprimary.
The levels are supplied before the groups so the last key is primary. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

Stability retains the order of complete key ties

Positions three and four have identical values for both supplied keys. The stable-sort contract preserves their original relative order, so three remains before four. Stability does not mean that every record stays near its original position, nor does it prevent records with different keys from moving across one another.

A payload label can make that tie visible. Attach fictional labels P, Q, R, S and T to the original positions. The sorted labels become S, T, Q, R and P. S remains before T because all supplied key comparisons tie. If an additional tie-breaking key is deliberately supplied, the formerly tied records may receive a new order because they no longer compare as equal on the complete key sequence.

One returned permutation must be applied to aligned payloads

The result is a permutation of positions, rather than a sorted copy of every associated field. Gather each record-aligned payload with the same returned positions. Sorting the group, level and payload arrays independently can destroy the correspondence between fields, even if each resulting array looks individually ordered.

For this example, the original group at position zero belongs with level two and payload P. The permutation moves that complete association to the final output position. It does not combine the smallest group with whichever level and payload happen to appear first after separate sorts. The preserved association is the reason an indirect result is useful.

SortedpairsA1,A1,A2,B1,B2havepayloadS,T,Q,R,P;positions3and4stayordered.
The output indices gather every aligned field rather than sorting each field independently. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

The returned integers name original positions, not persistent record identifiers. If the source arrays are subsequently filtered or reordered, reusing those positions against the changed arrays can gather different records. Preserve the source arrangement associated with a permutation when checking its result.

The sort axis belongs to each key array

The reference requires the keys to have the same shape. The leading dimension of a stacked keys argument enumerates which key is being supplied. The axis parameter identifies the axis inside each key along which separate slices are sorted; it does not count the outer sequence of keys as an extra data axis.

For several rows of aligned observations, sorting along each row produces a separate permutation for each row. Those local positions are interpreted within that row. Flattening every observation first would instead define a single global problem, with a different association structure. State the key shapes and chosen axis before applying a multidimensional result.

Direct value sorting and indirect multi-key sorting differ

The NumPy sort documentation describes returning a sorted array and its separate stability option. It also describes comparison order for structured fields. Those interfaces are related tools, but a normal two-dimensional array of key values is not automatically a collection of structured records with lexsort's explicit priority convention.

Likewise, applying argsort independently to a plain matrix containing both keys gives an ordering for each slice. It does not by itself create the single shared multi-key permutation in this example. Choosing an interface starts with deciding whether the objects being ordered are individual scalar values, structured records or aligned arrays of separate keys.

All numeric levels here are ordinary finite integers, and the group strings use a deliberately simple alphabet. Real applications must define missing-value handling and string comparison policy separately instead of interpreting an example as a business collation specification. Preserve key priority, complete ties and the common permutation when reviewing a result. Related material appears in the data-methods section and the technical article archive.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我