Use np.sort() to sort array values in an ordered sequence in Python. By using this you can sort an N-dimensional array of any data type. This function gives a sorted copy of the source array or input array without modifying the input array.
Note that Python language doesn’t have an array type but you can use the NumPy library to use arrays in Python. This article explains how to create and sort NumPy array.
If you are looking to sort a list or set, refer to sorting list in python or sorting sets in python.
1. Quick Examples of Sorting Arrays in Python
If you are in a hurry, below are some quick examples of how to sort array values in python.
# Quick examples of sorting arrays
# Example 1: Sort in ascending order
array = np.array([5,8,6,12,3,15,1])
sorted_array = np.sort(array)
# Example 2: Sort in descending order
array = np.array([5,8,6,12])
array[::-1].sort()
# Example 3: Use numpy.ndarray.sort()
array_copy = np.sort(array)[::-1]
2. Syntax of Array sort()
Following is the syntax of the numpy array sort(). The sort() returns a sorted copy of the input array, which is having the same shape and same type as an input.
# Syntax of NumPy array sort()
numpy.sort(arr, axis= -1, kind=None, order=None)
2.1 Parameter of sort()
This function allows four Parameters.
arr
: Array to be sorted.axis
: This parameter defines the axis along which sorting is performed. If this parameter is None, the array will be flattened before sorting, and by default, this parameter is set to -1, which sorts the array along with the last axis.order
: This parameter specifies which fields to compare first.kind
: [‘quicksort’{default}, ‘mergesort’, ‘stable’, ‘heapsort’]Sorting algorithm.
3. Python Sort Array Example
Use numpy.sort()
function to sort the elements of an array in an ordered sequence in Python. The parameter arr
is mandatory. If you execute this function on a one-dimensional array, it will return a one-dimensional sorted array containing elements in ascending order.
In order to use NumPy, first, you need to install it and import it by using import numpy as np
.
# Import NumPy module
import numpy as np
# Create NumPy array
array = np.array([5,8,6,12,3,15,1])
print("Original array:",array)
# Sort in ascending order
sorted_array = np.sort(array)
print("Sorted ascending order:",sorted_array)
Yields below output.
From the above code, it returned a sorted copy of the NumPy array, but the original NumPy remains unchanged.
4 Sort Array in Descending Order
By default numpy.sort()
function sorts the NumPy array in ascending order. Let’s see how to sort NumPy arrays in descending order.
Sorting a NumPy array in descending order sorts the elements from largest to smallest value. You can use the syntax array[::-1]
to reverse the array.
# Sort array in descending order
array = np.sort(array)[::-1]
print("Sorted descending order:\n",array)
# Output
# Sorted descending order:
# [15 12 8 6 5 3 1]
5. Sort String Arrays
Use this function to sort arrays of different data types like an array of strings, a boolean array, etc. When you sort an array with characters, it sorts in alphabetical order.
# Create NumPy array
array = np.array(["A","Z","X","M"])
print(array)
# Sort string of arrays
array = np.sort(array)
print(array)
# Output:
# ['A' 'Z' 'X' 'M']
# ['A' 'M' 'X' 'Z']
Frequently Asked Questions on Python Sort Array Values
You can use the sorted()
function or the sort()
method. The sorted()
function returns a new sorted list, while the sort()
method sorts the list in place.
You can use the reverse
parameter with sorted()
or sort()
to sort in descending order.
If you want to keep the original array unchanged, use sorted()
and store the result in a new variable.
Python’s built-in sorting functions primarily use an algorithm called Timsort, which is a hybrid sorting algorithm derived from merge sort and insertion sort. The sorted()
and sort()
functions use this algorithm.
You can use the key
parameter in sorted()
or sort()
to specify a function that returns a value by which to sort the objects.
Conclusion
In this article, I have explained how to sort arrays using the numpy.sort()
function in Python. And also I have explained how to sort string, number values by ascending and descending order.
Happy Learning!!
Related Articles
- How to Sort a List of Tuples in Python
- Python Sort List Descending
- Python Sort Dictionary by Key
- Python Sort List in Reverse Order
- Python Sort List of Numbers or Integers
- Python Sort List Alphabetically
- Sort using Lambda in Python
- How to Sort List of Strings in Python
- How to Sort Dictionary by Value in Python
- Sort Set of Values in Python
- Python Sort List of Lists
- How to Create an Array of Strings in Python?
- How to convert a list to an array?
- Python array explained with examples
- How to append an element to an array?
- How to add elements to an array?
- Difference between list and array in Python.
- How to get the array length?
- How to convert list to NumPy array?
- How to iterate over an array in Python?
- How to reverse an array in Python?