python - What is the practical application of argsort() -
recently came across argsort() , figured out return indexes of sorted sequence.but not clear @ 1 point, if using sort array why not use sort(). in [77]: import numpy np in [79]: arr = np.array([4,5,1,7,3]) in [80]: arr1 = np.sort(arr) in [81]: arr1 out[81]: array([1, 3, 4, 5, 7]) in [84]: arr2 = np.argsort(arr) in [85]: arr2 out[85]: array([2, 4, 0, 1, 3]) what benefits of argsort on normal sort? can use return result i.e indexes of sorted sequence. having idea of application in real world. numpy lets use array of indices index array: >>> import numpy np >>> arr = np.array([4,5,1,7,3]) >>> arr1 = np.sort(arr) >>> arr2 = np.argsort(arr) >>> arr1 = arr[arr2] >>> arr1 = np.sort(arr) >>> arr1 == arr[arr2] array([ true, true, true, true, true], dtype=bool) this can useful implementing decorate-sort-undecorate routine python's native sort function if give key function - example, sort square ...