NumPy Array Sorteren

Array Sorting

Sorting is the process of arranging elements in an ordered sequence.

An ordered sequence is any sequence that has an order corresponding to its elements, such as numbers or letters, in ascending or descending order.

The NumPy ndarray object has a named sort() The function, which sorts the specified array.

Example

Sort Array:

import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))

Run Example

Note:This method returns a copy of the array, while the original array remains unchanged.

You can also sort string arrays or any other data types:

Example

Sort Array Alphabetically:

import numpy as np
arr = np.array(['banana', 'cherry', 'apple'])
print(np.sort(arr))

Run Example

Example

Sort Boolean Array:

import numpy as np
arr = np.array([True, False, True])
print(np.sort(arr))

Run Example

Sort 2-D Array

If the sort() method is used on a two-dimensional array, two arrays will be sorted:

Example

Sort 2-D Array

import numpy as np
arr = np.array([[3, 2, 4], [5, 0, 1]])
print(np.sort(arr))

Run Example