Given a 2D NumPy array sort it by the 1st column. Print the final sorted array as a NumPy array only.

Given a 2d numpy array, sort it by the 1st column. print the final sorted array as a numpy array only. note: if two values in the 1st column are equal then the column in which the 2nd column value is lesser should come first. if the value in the second column is also the same then go to the third value and so on.

Example:
Input 1:
[[9 3 2]
 [4 0 1]
 [5 8 6]]

Output 2:
[[4 0 1]
 [5 8 6]
 [9 3 2]]

Input 2:

[[9 3 2]
 [4 0 1]
 [9 8 6]]

Output 2:

[[4 0 1]
 [9 3 2]
 [9 8 6]]

Explanation:

Example 1: Notice that the values in the first column are sorted. Also notice that the whole row should be moved and not just the individual values in the first column. For example, the row with the smallest value, i.e. 4 was moved as a whole. 

Example 2: Since this time the first row contains two 9s, the row in which the value in the second column is lesser came first.

NumPy Sort Array by First Column Then Second

# Reading the input list
import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)

# Import the NumPy package
import numpy as np

# Converting the list to a NumPy array
n_array = np.array(input_list)

# Sort the array using the argsort() argument. You can also specify the type of
# sort you need in the argsort() argument but it isn't necessary. Notice the 
# indexing is the main key here. The array is sorted by the row or column that you
# specify in the index
for i in range(len(n_array[0, :])):
    n_array = n_array[n_array[:,-1-i].argsort()]

# Print the final sorted array
print(n_array)

Comments