NumPy sort()

The `sort()` method sorts an array in ascending order.

Example

``````import numpy as np

array = np.array([10, 2, 9, -1])

# sort an array in ascending order
array2 = np.sort(array)

print(array2)

# Output : [-1  2  9 10]``````

sort() Syntax

The syntax of `sort()` is:

``numpy.sort(array, axis, order, kind)``

sort() Arguments

The `sort()` method takes four arguments:

• `array` - array to be sorted
• `axis` (optional) - axis along which the values are appended
• `order` (optional) - field to be compared
• `kind` (optional) - sorting algorithm

Notes:

• By default, `axis` is -1, the array is sorted based on the last axis.
• `kind` can be `quicksort` (default), `mergesort`, or `heapsort`.

sort() Return Value

The `sort()` method returns a sorted array.

Example 1: Sort a Numerical Array

``````import numpy as np

array = np.array([10.20, 2.10, 9.9, -1.4])

# sort the array in ascending order.
array2 = np.sort(array)

print(array2)``````

Output

`[-1.4  2.1  9.9 10.2]`

Example 2: Sort a String Array

``````import numpy as np

array = np.array(['Apple', 'apple', 'Ball', 'Cat'])

# sort a string array based on their ASCII values.
array2 = np.sort(array)

print(array2)``````

Output

`['Apple' 'Ball' 'Cat' 'apple']`

Example 3: Sort a Multidimensional Array

Multidimensional arrays are sorted based on the given axis.

``````import numpy as np

array = np.array([[3, 10, 2], [1, 5, 7], [2, 7, 5]])

# sort column wise based on the axis 1
array2 = np.sort(array)

# flattens the given array and sorts the flattened array
array3 = np.sort(array, axis = None)

# sort array row wise
array4 = np.sort(array, axis = 0)

print('Sorted based on last axis(1): \n', array2)
print('Sorted a flattened array', array3)
print('Sorted based on axis 0: \n', array4)``````

Output

```Sorted based on last axis(1) :
[[ 2  3 10]
[ 1  5  7]
[ 2  5  7]]

Sorted a flattened array [ 1  2  2  3  5  5  7  7 10]

Sorted based on axis 0 :
[[ 1  5  2]
[ 2  7  5]
[ 3 10  7]]```

When sorting based on axis 0, rows are compared. The elements in the first column are sorted first followed by the second column and so on. All columns are sorted independently of each other.

Example 4: Sort an Array With order Argument

``````import numpy as np

datatype = [('name', 'U7'), ('age', int), ('height', int)]
people = [
('Alice', 25, 170),
('Bob', 30, 180),
('Charlie', 35, 175)
]
array = np.array(people, dtype = datatype)

# sort the array based on height
array2 = np.sort(array, order = 'height')

print(array2)``````

Output

`[('Alice', 25, 170) ('Charlie', 35, 175) ('Bob', 30, 180)]`

Example 5: Sort an Array With Multiple order Argument

``````import numpy as np

datatype = [('name', 'U7'), ('age', int), ('height', int)]
people = [
('Alice', 25, 170),
('Bob', 30, 180),
('Charlie', 35, 175),
('Dan', 40, 175),
('Eeyore', 25, 180)
]
array = np.array(people, dtype = datatype)

# sort the people array on the basis of height
# if heights are equal, sort people on the basis of age
array2 = np.sort(array, order = ('height', 'age'))

print(array2)``````

Output

`[('Alice', 25, 170) ('Charlie', 35, 175) ('Dan', 40, 175) ('Eeyore', 25, 180) ('Bob', 30, 180)]`

The kind Argument

The `kind` argument is used in NumPy `sort()` to specify the algorithm used for sorting. For example,

``````import numpy as np

array = np.array([10, 2, 9, 1])

# sort an array in ascending order by quicksort algorithm
array2 = np.sort(array, kind = 'quicksort')

print(array2)

# Output : [1 2 9 10]``````

The `kind` argument can take several values, including,

• quicksort (default): This is a fast algorithm that works well for most cases i.e. small and medium-sized arrays with random or uniformly distributed elements.
• mergesort: This is a stable, recursive algorithm that works well for larger arrays with repeated elements.
• heapsort: This is a slower, but guaranteed O(n log n) sorting algorithm that works well for smaller arrays with random or uniformly distributed elements

The `kind` argument has no direct impact on the output but it determines the performance of the method.