Python
Python list sort method sorts the elements of the list in the given order. The syntax of the sort function is given below.
Sort the list of tuples by the first element
A list of tuples can be sorted by the first element of the tuple, i.e., sorted by key value using the following code. Use item getter from the operator module to sort values in the list of tuples.
First, let’s create a list of tuples i.e.
List = [(‘First’, 10), (‘Second’, 5), (‘Third’, 6)]
Output: [(‘First’, 10), (‘Second’, 5), (‘Third’, 6)]
Now, we will use itemgetter to sort above list by first element of tuple i.e. key value using following code.
from operator import itemgetter
Initialize list
Lis = [(‘a’, 2), (‘b’, 3), (‘c’, 1)]
# Sort the list with respect to the first element
Lis .sort(key = itemgetter(0))
# printing modified contents
print (“Contents of the list in sorted order w.r.t 1st element : “)
print (Lis)
Use the sorted() function
The easiest way to sort a list of tuples by first element is to use the sorted() function. This function takes a list and returns a new sorted list. The sorted() function can be used with lists of numbers, strings, or tuples.
If you have a list of tuples, you can sort them by the first element in each tuple with the following code:
tuple_list = [(‘a’,3), (‘b’,1), (‘c’,2)]
sorted_list = sorted(tuple_list, key=lambda x: x[0])
print(sorted_list)
[(‘a’, 3), (‘b’, 1), (‘c’, 2)]
Use the key parameter
If you want to sort a list of tuples by the first element of each tuple, you can use the sorted() function and pass in a key=lambda x: x[0] argument.
For example:
a = [(1,2), (3,1), (5,4), (2,6)]
sorted(a, key=lambda x: x[0])
[(1, 2), (2, 6), (3, 1), (5, 4)]
Java
To sort a list of tuples by the first element in each tuple, you can use the following code:
Sort list of tuples by first element
To sort a list of tuples by the first element in each tuple, we can use the sorted() function.
For example, let’s say we have the following list of tuples:
unsorted_list = [(‘b’, 2), (‘a’, 1), (‘c’, 3)]
We can sort this list of tuples by the first element in each tuple like this:
sorted_list = sorted(unsorted_list, key=lambda x: x[0])
print(sorted_list) # [(‘a’, 1), (‘b’, 2), (‘c’, 3)]
Use the Collections.sort() method
To use the Collections.sort() method you need to do the following:
- import java.util.*;
- have your class implement the Comparable interface
- override the compareTo() method in your class
- call Collections.sort() and pass in a List of objects
Use a Comparator
A comparator is an interface that defines how two objects are compared. To use a comparator, you need to implement the Comparator interface and override the compare method. In this method, you need to specify how the two objects are compared. If the return value is 0, it means that the two objects are equal. If it is a negative value, it means that the first object is less than the second object. If it is a positive value, it means that the first object is greater than the second object. You can use a Comparator to sort objects in either ascending or descending order.
In the following example, we have a class named Employee which has two fields: name and salary. We will use a comparator to sort employees by their salary in ascending order:
import java.util.*;
class Employee {
String name;
int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public int getSalary() {
return salary;
}
public static void main(String args[]) {
ArrayList<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Annie", 20000)); // only these 2 lines will change everytime we want to test our program with different input values (remember single responsibility principle?)
list.add(new Employee("Ben", 15000)); // same here... we can also create another class for reading data from a file or something like that... but for simplicity lets just stick with manual input here :)
Collections.sort(list, new Comparator<Employee>(){ //to sort using Collections.sort(), we need to pass in 2 arguments: 1)the list we want to sort and 2)the comparator object which defines how sorting should be done @Override //Override is an annotation which tells us that we are overriding the compare method of Comparator interface
@Override //we use @Override so that if we make any mistakes while overriding... compiler will immediately give us an error :)
public int compare(Employee e1, Employee e2){ //this compare method takes in 2 arguments of type Employee (which we want to sort)... and compares them based on salary.. see below.. if(e1.getSalary() > e2</p>