How to Sort 2D Array in Python

To sort 2D array in Python, use either sort() or sorted() function. The sort() is a built-in list method that sorts the list ascending by default. It sorts the list by the length of the values.

The 2D array is a type of array in which an array consists of several arrays inside it. Generally, we can sort the one-dimensional array by simply using the sort() function, but for sorting the 2D array, we can give the column based on which we want to sort.

Python does not have a built-in array data type, but we can portray the list as an array in this example.

Using sort() function

The list sort() is a built-in Python function that can sort a List in ascending, descending, or user-defined order.

Syntax

``list.sort(reverse=True|False, key=myFunc)``

Arguments

 reverse It is an optional argument. The reverse=True will sort the list descending. Default is reverse=False key It is an optional argument. It is a function to specify the sorting criteria(s)

Program to sort the 2d array using the sort() function

``````list = [['Raj', 10], ['Ram', 8], ['Anil', 3], ['Shyam', 7]]
list.sort()
print(list)``````

Output

``[['Anil', 3], ['Raj', 10], ['Ram', 8], ['Shyam', 7]]``

In this program, we created an array in which we consist of several other arrays. Each array inside the outer array consists of name and age. Then we used the sort function to sort the array.

The sort() function sorts the array based on the first column, the name column. We can see the output as:

In this example, we can see that an array is sorted based on the name column. Now, let’s see how to sort the array based on other columns.

``````lis = [['Raj', 10], ['Ram', 8], ['Anil', 3], ['Shyam', 7]]
lis.sort(key=lambda i: i[1])
print(lis)``````

Output

``[['Anil', 3], ['Shyam', 7], ['Ram', 8], ['Raj', 10]]``

We used the same list as input. We have a sort() function to sort the array in the next line. Inside the function argument, we used the key as the first column. This key describes the column based on which the sort function should sort.  In this program, we saw how to sort the array based on the age column.

Using sorted() function to sort 2D array

The sorted() is a built-in Python function that returns a sorted list of the defined iterable object. So, for example, we can use the sorted() function to sort the 2D arrays.

``````lis = [['Raj', 10], ['Ram', 8], ['Anil', 3], ['Shyam', 7]]
l = sorted(lis, key=lambda i: i[1])
print(l)``````

Output

``[['Anil', 3], ['Shyam', 7], ['Ram', 8], ['Raj', 10]]``

In this program, we used the sorted function to sort the array. These sorted values should be stored in a new variable. Hence the sorted array is stored in a variable called l. The l consists of a sorted array.

Conclusion

You have two options for sorting a 2D array in Python. One is sort() and another is sorted() function.

That’s it for this tutorial.

Related posts

How to Filter List in Python

How to Save Array to File in Python

How to Check If array is empty in Python