In this Python NumPy tutorial, we will discuss Python sort NumPy array and also cover the below examples:
- NumPy sort() function
- Python sort NumPy array
- Python sort NumPy array by column
- Python sort NumPy array descending
- Python sort NumPy array get index
- Python sort NumPy array alphabetically
- Python sort NumPy array of strings
- Python sort NumPy array by row
- Python sort 2d NumPy array by column
- Python sort two NumPy arrays
NumPy sort() function
- The NumPy sort() method can sort numpy arrays from low values to high values.
- To use the NumPy sort function we will create a new script with the NumPy library imported as np.
- To sort the NumPy array we can use the function numpy.sort().
- Sorting means putting values in an ordered iterable sequence.
- This function always returns a sorted copy of the source numpy array, which will have the same shape and dtype as a source NumPy array.
Read: Python NumPy matrix
Python sort NumPy array
- In this section, we will learn about python sort the NumPy array.
- First, we will learn and discuss numpy arrays and matrices because this comes under the NumPy library.
- Numpy is a library that allows us to create multi-dimensional arrays.
Syntax:
Here is the Syntax of sort numpy array
numpy.sort(
a,
axis=-1
order=None
)
- a: array_like array is to be sorted
- axis: Axis along which to sort the numpy array.If None, the numpy array is flattened before sorting. The default value is -1 which sorts the elements along the last given axis.
- order: When a numpy array is defined, this parameter specifies which fields to compare first or second. A single field can be given as a string, and not all fields need to be specific but unspecified fields will still be used, in the presence in which they come up in the datatype.
Example:
import numpy as np
a = np.array([1,4,6,2,3,9])
c = (np.sort(a))
print(c)
Here is the Screenshot of following given code
This is how to sort an array using Python NumPy.
Read: Python NumPy append
Python sort NumPy array by column
- In this section, we will learn about python sort NumPy array by column. To sort the column elements in an array we can use the method np. sort().
- The output shows that the smallest value in the first column is at position 0.
Syntax:
Here is the Syntax of sort numpy array by column
numpy.sort(
a,
axis=0
order=None
)
Example:
import numpy as np
a = np.array([[1,7,4],
[4,2,2],
[3,2,8]])
c = np.sort(a, axis=0)
print(c)
Here is the Screenshot of following given code.
This is how to sort NumPy array by column in Python.
Read: Python NumPy arange
Python sort numpy array descending
- In this section, we will learn about python sort NumPy array descending.
- To sort the array elements in descending order we can use the method NumPy np.sort().
Syntax:
numpy.sort(
a,
axis=0
order=None
)
Example:
import numpy as np
a = np.array([6, 7, 23, 27, 13, 65, 91])
sorted_array = np.sort(a)[::-1]
print(sorted_array)
Here is the Screenshot of following given code
This is how to sort numpy array in descending in Python.
Read: Python NumPy Sum
Python sort NumPy array get index
- In this section, we will learn about python sort NumPy array get index.
- To get the index we can easily use the function numpy.argsort().
- The numpy argsort() function is used to return the indices that can be used to sort an array.
- The returned array contains the indices along the given axis in sorted order.
- This function returns two indices that would start an array to perform indirect sort along the given axis using the algorithm.
Syntax:
Here is the Syntax of numpy.argsort()
numpy.argsort
(
a,
axis=-1,
order=None
)
Example:
import numpy as np
a = np.array([6, 7, 23, 27, 13])
sorted_array = (np.argsort(a))
print(sorted_array)
Here is the Screenshot of following given code
This is an example of Python sort NumPy array get index.
Read: Python NumPy zeros
Python sort numpy array by alphabetically
- In this section, we will learn about python sort NumPy array alphabetically.
- By alphabetically order we mean the characters of the string are supposed to start from A to Z.
- So the logic we would use to implement that is the np. sort () function.
Syntax:
Here is the Syntax of sort numpy array by alphabetically.
numpy.sort(
a,
axis=0
order=None
)
Example:
import numpy as np
a = np.array(['b', 'a', 'h', 'e', 'k'])
sorted_array = (np.sort(a))
print(sorted_array)
Here is the Screenshot of the following given code.
This is how to sort numpy array alphabetically in Python.
Read Python NumPy shape with examples
Python sort numpy array of strings
- In this section, we will learn about Python sort a NumPy array of strings. The returned array stores the indices along the given axis in sorted order elements.
- So the logic we would use to implement that is the Python np.sort() function.
Syntax:
Here is the Syntax of sort numpy array by alphabetically.
numpy.sort(
a,
axis=0
order=None
)
Example:
import numpy as np
a = np.array(['John', 'Micheal', 'George'])
sorted_array = (np.sort(a))
print(sorted_array)
Here is the Screenshot of following given code.
This is how to sort numpy array of strings in Python.
Read: Check if NumPy Array is Empty in Python
Python sort numpy array by row
- In this section, we will learn about python sort NumPy array by row.
- Suppose we have a NumPy array and we want to sort the rows according to the order of elements.
- We can do this with the Python numpy.sort() function.
- This function always returns a sorted copy of the source numpy array, which will have the same shape and dtype as a source numpy array. To sort the row values in an array we can use the function np.sort().
Syntax:
Here is the Syntax of sort numpy array by row
numpy.sort(
a,
axis=1
order=None
)
Example:
import numpy as np
a = np.array([[1,7,4],
[4,2,2],
[3,2,8]])
c = np.sort(a, axis=1)
print(c)
Here is the Screenshot of the following given code.
This is how to sort numpy array by row in Python.
Read Python NumPy Stack
Python sort 2d numpy array by column
- In this section, we will learn about python sort 2d NumPy array by column.
- Two Dimensional array means the collection of homogenous data or numbers in lists of a list. It is also known as a 2d matrix. In a 2D matrix, you have to use two square brackets that is why it said lists of lists.
- Suppose we have a NumPy array and we want to sort the columns according to the order of elements.
- We can do this with the numpy.sort() function. To sort the column values in an numpy array we can easily use the function np.sort().
Syntax:
Here is the Syntax of sort numpy array by column
numpy.sort(
a,
axis=0
order=None
)
Example:
import numpy as np
a = np.array([[1,7,4],
[4,2,2]])
c = np.sort(a, axis=0)
print(c)
Here is the Screenshot of following given code
This is how to sort 2d numpy array by column in Python.
Read: Python NumPy Random
Python sort two NumPy arrays
- In this section, we will learn about python sort two NumPy array.
- To use the NumPy sort function we will create a new script with the NumPy library imported as np.
- To sort the NumPy array we can easily use the function np. sort(). Sorting means inserts values in an ordered iterable sequence.
Syntax:
Here is the Syntax of sort two numpy array
numpy.sort(
a,
axis=-1
order=None
)
Example:
import numpy as np
a = np.array([[1,7,2],
[7,6,2]])
c = np.sort(a)
print(c)
Here is the Screenshot of following given code
This is how to sort two NumPy arrays in Python.
You may Python tutorials:
- Python Tkinter OptionMenu
- Extract text from PDF Python
- Registration form in Python using Tkinter
- Convert float to int Python
- Python NumPy where
- Convert string to float in Python
In this Python tutorial, we will discuss Python sort numpy array and also cover the below examples:
- Python sort() function
- Python sort NumPy array
- Python sort NumPy array by column
- Python sort NumPy array descending
- Python sort NumPy array get index
- Python sort NumPy array alphabetically
- Python sort NumPy array of strings
- Python sort NumPy array by row
- Python sort 2d NumPy array by column
- Python sort two NumPy arrays
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.