# a.sort (), sorted (a), np.argsort (a) and np.lexsort (b, a) in Python

| |

Let & # 39; a & # 39; be an array

` # Python code to sort the array in place `
` # using a.sort `

` import ` ` numpy as np `

` # Numpy array created `

` a ` ` = ` ` np.array ([` ` 9 ` `, ` ` 3 ` `, ` ` 1 ` `, ` ` 7 ` `, ` ` 4 ` `, ` ` 3 ` `, ` ` 6 ` `]) `

` `
` # unsorted print array `

` print ` ` (` `’ Original array: ’` `, a) `

` `
` # Return type - None `

` print ` ` (` ` ’Return type:’ ` `, a.sort ()) `

` # Sorted output array `

` print ` ` (` ` ’Original array sorted-"’ ` `, a) `

` OUTPUT: For a.sort () Original array: [9 3 1 7 4 3 6] Return type: None Original array sorted-" [1 3 3 4 6 7 9] `
• sorted (a)
(i) Creates a new list from the old and returns a new sorted
(ii) Return type — list
(iii) Takes up more space because a copy of the original array is created and then sorted.
(iv) Slower than a.sort ()

 ` # Python code to create a sorted copy using ` ` # sorted () ` ` import ` ` numpy as np `   ` # Numpy array created ` ` a ` ` = ` ` np.array ([` ` 9 ` `, ` ` 3 ` `, ` ` 1 ` `, ` ` 7 ` `, ` ` 4 ` `, ` ` 3 ` `, ` ` 6 ` `]) ` ` `  ` # unsorted print array ` ` print ` ` (` ` ’Original array:’ ` `, a) ` ` b ` ` = ` ` sorted ` ` (a) `   ` # the sorted list is returned to b, type b ` ` # "class & # 39; list & # 39;" ` ` print ` ` (` ` ’New array sorted-"’ ` `, b) `   ` # original array unchanged ` ` print ` ` (` `’ Original array-" ’` `, a ) `

` OUTPUT: a.sorted () Original array: [9 3 1 7 4 3 6] New array sorted -" [1, 3, 3, 4, 6, 7, 9] Original array-" [9 3 1 7 4 3 6] `
• np.argsort (a)
(i) Returns the indices that will sort the array
(ii) Return type — numpy array
(iii) Takes up space as a new array of sorted indices is returned.

 ` # Python code to demonstrate how np.argsort works ` ` import ` ` numpy as np `   ` # Numpy array created ` ` a ` ` = ` ` np.array ([` ` 9 ` `, ` ` 3 ` `, ` ` 1 ` `, ` ` 7 ` `, ` ` 4 ` `, ` ` 3 ` `, ` ` 6 ` `] ) ` ` `  ` # unsorted print array ` ` print ` ` (` ` ’Original array:’ ` `, a) `   ` # Sort array indices ` ` b ` ` = ` ` np.argsort (a) ` ` print ` ` (` `’ Sorted indices of original array-" ’` ` , b) `   ` # Get a sorted array using sorted indices ` ` # c is a temporary array created from the same len as b ` ` c ` ` = ` ` np.zeros (` ` len ` ` (b), dtype ` ` = ` ` int ` `) ` ` for ` ` i ` ` in ` ` range ` ` (` ` 0 ` `, ` ` len ` ` (b)): ` ` c [i] ` ` = ` ` a [b [i]] ` ` print ` ` (` `’ Sorted array-" ’` `, c) `

` OUTPUT: np.argsort (a) Original array: [9 3 1 7 4 3 6] Sorted indices of original array-" [2 1 5 4 6 3 0] Sorted array-" [1 3 3 4 6 7 9] `
• np.lexsort ((b, a))
(i) Perform indirect sort using sequence keys
(ii) Sort by a, then by b
(iii) Return type ndarray of ints Array of indices that sort the keys along the specified axis
(iv) Takes up space as a new one is returned array of sorted indices.

` OUTPUT: np.lexsort ((b, a)) column a, column b 9 4 3 6 1 9 3 2 4 1 3 8 6 7 Sorted indices -" [2 3 1 5 4 6 0] `

This article courtesy of SHAURYA UPPAL . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

## Shop Learn programming in R: courses

\$ Best Python online courses for 2022

\$ Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method

 ` # Python code to demonstrate how it works ` ` # np.lexsort () ` ` import ` ` numpy as np `   ` # Numpy array created ` ` a ` ` = ` ` np.array ([` ` 9 ` `, ` ` 3 ` `, 1 , 3 , 4 , 3 , 6 ]) # First column `` b = np.array ([ 4 , 6 , 9 , 2 , 1 , 8 , 7 ]) # Second column print ( ’column a, column b’ ) for (i, j) in zip (a, b): print (i, ’ ’ , j)    ind = np.lexsort ((b, a)) # Sort by then by b print ( ’Sorted indices-"’ , ind) `