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

Ordering of data elements in a specific order is a frequently needed operation. To sort elements in an array, python uses the functions named sorted() and array.sort().

## sorted(array)

This function returns a sorted array without modifying the original array.

a = [9,5,3,1,12,6]
b = sorted([9,5,3,1,12,6])
print "Sorted Array :\n",
print (b)
print "Original Array :\n",
print (a)

Running the above code gives us the following result −

Sorted Array :
[1, 3, 5, 6, 9, 12]
Original Array :
[9, 5, 3, 1, 12, 6]

## list.sort()

The sort function returns a sorted array by doing in-place modification to the array supplied. Hence the original array gets modified as shown in the example below.

a = [9,5,3,1,12,6]
print "Original Array :\n",
print (a)
print "Sorted Array :\n",
a.sort()
print (a)

Running the above code gives us the following result −

Original Array :
[9, 5, 3, 1, 12, 6]
Sorted Array :
[1, 3, 5, 6, 9, 12]

So sorted() function is slower than sort() as it creates a copy of the original array and then modifies it.

More complex sorting requirements are done using Numpy. Numpy is a python library which is extensively used in scientific data processing as it provides lots of advanced features. We will see both pure python sorting methods and Numpy sorting methods in the below examples.

## numpy.argsort

This function in numpy returns the indices of the sorted array instead of the array elements. In the below example we take the array, print its elements along with the index for each element. Then we apply the argsort function which gives us the indices of sorted array as a result and the result is also an array.

import numpy as np
x = np.array([9,5,3,1,12,6])
print(x)

#Print the positions of elements
for i in range(len(x)):
print "[",i,"]",x[i],
print "\n"
# Print the indices of sorted elements
s = np.argsort(x)
print(s)

Running the above code gives us the following result −

[ 9 5 3 1 12 6]

[ 0 ] 9 [ 1 ] 5 [ 2 ] 3 [ 3 ] 1 [ 4 ] 12 [ 5 ] 6

[3 2 1 5 0 4]

## numpy.lexsort

This function is used for sorting using multiple sort keys involving more than one array. For example, we first sort data in Column A and then sort the values in column B. In the below example we take two arrays representing column A and column B. On applying lexsort() function for sorting first by column A and then by column B we get the result of sorting as an array containing the indices of the elements in column A.

import numpy as np
colA = [2,5,1,8,1] # First column
colB = [9,0,3,2,0] # Second column
# Sort by ColA and then by colB
sorted_index = np.lexsort((colB,colA))
print(sorted_index)
#print the result showing the
#column values as pairs
print [(colA[i],colB[i]) for i in sorted_index]

Running the above code gives us the following result −

[4 2 0 1 3]
[(1, 0), (1, 3), (2, 9), (5, 0), (8, 2)]

As you can see the lowest two values in colA are 1 and 1 at the index position 2 and 4. But the result shows 4 and 2 as the ascending order as the respective values in column B which are 0 and 3 are also sorted as first 0 then 3, making the result as 4 and 2.