NumPy argsort returns the original positions that arrange values in sorted order
Sorting values and finding their sorting positions answer different questions. NumPy sort returns arranged values, while argsort returns indices describing which original positions to read and in what order. Academia Master Trader explains argsort in English for readers in Germany. The examples use small constructed arrays and zero based indices. The aim is to preserve the relationship between an entry and its original position while explaining a numerical ordering.
Read the returned numbers as positions
After import numpy as np, define x = np.array([8,2,6,4]). The call idx = np.argsort(x) returns [1,3,2,0]. These numbers are indices, not the sorted values. The smallest entry two is at original index one. Four is at index three, six at index two, and eight at index zero.
Using those indices, x[idx] returns [2,4,6,8]. Read the instruction as: take x at index one, then three, then two, then zero. The output from argsort gives the order in which to visit the original positions. It does not give each original entry's rank at that same position.

Follow the diagram from values to positions
The figure shows the original values, the argsort indices and the values retrieved through those indices. The middle row begins with one because the first sorted value comes from original position one. Its final zero points back to the original eight. Keeping all rows labelled prevents the index sequence from being mistaken for another set of measurements.
The call does not change x. Saving the indices separately keeps the original sequence available and gives an explicit arrangement to apply where appropriate. That is useful when explaining how sorted values relate to the source, rather than displaying the sorted numbers without their positional history.
Keep associated labels aligned
Suppose labels = np.array(['A','B','C','D']), where each label belongs to the entry at the same original position in x. The expression labels[idx] returns ['B','D','C','A']. Applying the same index order to both arrays keeps each label with its corresponding value.
This only works when the original arrays really share that positional relationship. A label array from another sequence does not become correctly matched merely because it has four entries. Establish the association first, then apply the indices. Sorting values alone and leaving labels in their original order would describe different pairings.
For matrices, indices belong to the selected axis
Define a = np.array([[8,2,6],[3,9,1]]). The call np.argsort(a,axis=1) returns [[1,2,0],[2,0,1]]. Each row contains column indices for sorting that row. The first row should read columns one, two and zero; the second should read columns two, zero and one.
Apply the indices with np.take_along_axis(a,np.argsort(a,axis=1),axis=1) to obtain [[2,6,8],[1,3,9]]. The function name is written exactly here because the operation applies indices along a named axis. Ordinary one dimensional indexing syntax should not be assumed to mean the same thing for a matrix.
Make ties and flattening explicit
For repeated values, np.argsort([5,2,5],kind='stable') returns [1,0,2]. A stable sort preserves the original relative order of entries comparing equal. The two fives therefore appear through original positions zero and two in that order. Without a stable ordering choice, that tie order is not guaranteed.
Choosing axis=None makes argsort work on the flattened input. Those indices then refer to flattened positions, not directly to row and column pairs. Name the intended axis before interpreting the returned integers.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the original values, index convention, sorting axis and any stability requirement. Then retrieve the values with the returned indices. Those checks distinguish sorting positions from values, ranks and any claim about future observations.








