What is the numpy.msort() function from NumPy in Python?

Overview

The msort() function in Python is simply used to return a copy of an array sorted along the first axis.

Syntax

numpy.msort(a)

Parameter value

The msort() function takes a single and mandatory parameter value a, which represents the input array to be sorted.

Return array

The msort() function returns an array of the same type and shape as the input array a passed to it.

Let’s see how to use to it through the following code example.

Example

import numpy as np
# creating lists of numbers
myarray = np.array([[3, 5, 2, 4, 1], [9, 6, 10, 8, 7]])
# implementing the lexsort() function to sort mylist1 first
sortedarray = np.msort(myarray)
print(sortedarray)

Code explanation

  • Line 1: We import the numpy module.
  • Line 3: We create a 2D array using the array() function
  • Line 7: We implement the msort() function on the array myarray. The result is assigned to a variable sortedarray.
  • Line 9: We print the newly sorted array variable, sortedarray.

Free Resources