**Numpy. Sort() function**

This function provides a variety of sorting functions, and supports a variety of sorting algorithms such as merge sort, heap sort, fast sort, etc

The format of using the numpy. Sort() method is:

numpy.sort(a,axis,kind,order)

- a: Array to sort
- Axis: along the sorting axis, axis = 0 is sorted by column, axis = 1 is sorted by row.
- Kind: the algorithm used for sorting. Fast sorting is used by default. Common sorting methods include
- Quicksort: fast sort, fastest, algorithm is not stable
- Merge sort: merge sort has the advantages of stability and high space complexity, which is generally considered in external sort
- Heap sort: heap sort has the advantage that in the worst case, its time complexity is also o (nlogn), which is the most efficient and space saving sorting method

- Order: if it contains fields, it indicates the fields to be sorted (such as sorting by an element item in the array)

Here is an example to understand the usage of numpy. Sort() function

Suppose we have a set of user information, including the user name and the age of the user. We sort by the age of the user

```
dt=np.dtype([('name','S20'),('age','i4')])
a=np.array([('adm','19'),('wan','23'),('ade','23')],dtype=dt)
s=np.sort(a,order='age',kind='quicksort')
print(s)
```

Operation result:

[(b’adm’, 19) (b’ade’, 23) (b’wan’, 23)]

Process finished with exit code 0

**Numpy. Argsort() function**

Index of the smallest to largest element returned by the numpy.argsort() function

It can be better understood through the following examples

Use the argsort () method to return the index and refactor the array

```
x=np.array([3,8,11,2,5])
Print ('return index from small to large ')
y=np.argsort(x)
print(y)
Print ('sort the original array by index ')
print(x[y])
Print ('refactor original array ')
for i in y:
print(x[i],end=",")
```

Operation result:

Return index from small to large

[3 0 4 1 2]

Sort the original array by index

[ 2 3 5 8 11]

Reconstruct the original array

2,3,5,8,11,

Process finished with exit code 0

**Numpy. Lexport() function**

The numpy. Sort() function can sort multiple sequences. For example, when comparing scores, we compare the total scores first, and then the priority from the last column to the front row. Then we use the lexport() method

```
nm = ('raju','anil','ravi','amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
ind = np.lexsort((dv,nm))
Print ('call lexport() function: ')
print (ind)
print ('\n')
Print ('use this index to get sorted data: ')
print ([nm[i] + ", " + dv[i] for i in ind])
```

Operation result:

Use this index to get the sorted data:

[‘amar, f.y.’, ‘anil, s.y.’, ‘raju, f.y.’, ‘ravi, s.y.’]Process finished with exit code 0

**Numpy. Partition() function**

Numpy. Partition () is called partition sorting. You can make a number to partition the array.

The format is as follows:

```
partition(a,kth[,axis,kind,order])
```

Example: the implementation puts the elements smaller than 7 in the array at the front and the elements larger than 7 at the back

```
#Partition partition sorting
a=np.array([2,3,9,1,0,7,23,13])
print(np.partition(a,7))
```

Operation result:

[ 0 1 2 3 7 9 13 23]

Process finished with exit code 0

Example: the implementation puts the elements smaller than 7 in the array in the front, the elements larger than 10 in the back, and the elements between 7-10 in the middle

Partition partition sorting

```
a = np.array([2, 3, 9, 1, 6, 5, 0, 12, 10, 7, 23, 13, 27])
print(np.partition(a, (7, 10)))
print(np.partition(a, (2, 7)))
```

Operation result

[ 1 0 2 3 5 6 7 9 10 12 13 23 27]

[ 0 1 2 6 5 3 7 9 10 12 23 13 27]Process finished with exit code 0

Note: the position of 10 in (7, 10), the value cannot exceed the array length.

**Numpy. Nonzero() function**

Returns the index of a non-zero element in an input array

```
a = np.array([[30,40,0],[0,20,10],[50,0,60]])
Print ('Our array is: ')
print (a)
print ('\n')
Print ('call nonzero() function: ')
print (np.nonzero (a))
```

Operation result:

Our array is:

[[30 40 0]

[ 0 20 10]

[50 0 60]]Call the nonzero() function:

(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))

Process finished with exit code 0

**Numpy. Where() function**

Returns the index that meets the input criteria

```
Use of where() function
b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27])
y = np.where(b > 10)
print(y)
Print ('Get elements in array by index ')
print(b[y])
```

Operation result:

(array([6, 7, 8], dtype=int64),)

Using index to get elements in array

[23 13 27]Process finished with exit code 0

**Numpy. Extract() function**

The function numpy. Extract () implements elements that return custom conditions

```
#Extract() custom element filtering
b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27])
con = np.mod(b, 2) == 0
y = np.extract(con, b)
print(a[y])
```

Operation result:

[9 2 6]

Process finished with exit code 0

**Other sort functions**

The numpy. Argmax() and numpy. Argmin() functions return the index of the maximum and minimum elements along a given axis, respectively. The numpy. Sort ﹣ complex (a) function implements the sorting of complex numbers according to the order of real parts first and then virtual parts. The function numpy.argpartition (a, kth [, axis, kind, order]) implements partitioning arrays along the specified axis by specifying keywords.

Here is an example of a complex ordering:

```
t = np.array([ 1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j])
res = np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j])
print(res)
```

Operation result:

[1.+2.j 2.-1.j 3.-3.j 3.-2.j 3.+5.j]

Process finished with exit code 0

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.