Wednesday, 22 September 2021

Python: Sort list of items with custom choice

Python allows you to customize the sorting behaviour. For example, I may want to sort list of employees by their firstName.

 

Step 1: Define a comparator function.

def by_firstName_comparator(emp):
    return emp.firstName

Step 2: Specify the comparator function to sort the items by firstName.

my_emps.sort(key=by_firstName_comparator)


Find the below working application.

 

sort_with_custom_choice.py

def by_id_comparator(emp):
    return emp.id

def by_firstName_comparator(emp):
    return emp.firstName

def by_lastName_comparator(emp):
    return emp.lastName

class Employee:
    def __init__(self, id, firstName, lastName):
        self.id = id
        self.firstName = firstName
        self.lastName = lastName

    def __str__(self):
        return str(self.id)+"," +self.firstName + ","+self.lastName

def print_list(my_list, msg):
    print(msg)
    for item in my_list:
        print(item)
    print()

emp1 = Employee(1, "Krishna", "Gurram")
emp2 = Employee(21, "PTR", "Nayan")
emp3 = Employee(3, "Sankalp", "Dubey")

my_emps = [emp1, emp2, emp3]

print_list(my_emps, 'Actual list content')

my_emps.sort(key=by_id_comparator)
print_list(my_emps, 'Sort by id')

my_emps.sort(key=by_firstName_comparator)
print_list(my_emps, 'Sort by firstName')

my_emps.sort(key=by_lastName_comparator)
print_list(my_emps, 'Sort by lastName')


Output

Actual list content
1,Krishna,Gurram
21,PTR,Nayan
3,Sankalp,Dubey

Sort by id
1,Krishna,Gurram
3,Sankalp,Dubey
21,PTR,Nayan

Sort by firstName
1,Krishna,Gurram
21,PTR,Nayan
3,Sankalp,Dubey

Sort by lastName
3,Sankalp,Dubey
1,Krishna,Gurram
21,PTR,Nayan


You can achieve the same behaviour using sorted function also.

 

Example

my_emps.sort(key=by_id_comparator)


Find the below working application.

 

sorted_demo_1.py

def by_id_comparator(emp):
    return emp.id

def by_firstName_comparator(emp):
    return emp.firstName

def by_lastName_comparator(emp):
    return emp.lastName

class Employee:
    def __init__(self, id, firstName, lastName):
        self.id = id
        self.firstName = firstName
        self.lastName = lastName

    def __str__(self):
        return str(self.id)+"," +self.firstName + ","+self.lastName

def print_list(my_list, msg):
    print(msg)
    for item in my_list:
        print(item)
    print()

emp1 = Employee(1, "Krishna", "Gurram")
emp2 = Employee(21, "PTR", "Nayan")
emp3 = Employee(3, "Sankalp", "Dubey")

my_emps = [emp1, emp2, emp3]

print_list(my_emps, 'Actual list content')

my_emps.sort(key=by_id_comparator)
print_list(my_emps, 'Sort by id')

my_emps.sort(key=by_firstName_comparator)
print_list(my_emps, 'Sort by firstName')

my_emps.sort(key=by_lastName_comparator)
print_list(my_emps, 'Sort by lastName')


Output

Actual list content
1,Krishna,Gurram
21,PTR,Nayan
3,Sankalp,Dubey

Sort by id
1,Krishna,Gurram
3,Sankalp,Dubey
21,PTR,Nayan

Sort by firstName
1,Krishna,Gurram
21,PTR,Nayan
3,Sankalp,Dubey

Sort by lastName
3,Sankalp,Dubey
1,Krishna,Gurram
21,PTR,Nayan


Instead of passing a function, you can pass lambda to the sort method and sorted function.

 

Example 1: Sort by id.

my_emps.sort(key=lambda emp : emp.id)
print_list(my_emps, 'Sort by id')


Example 2: Sort by firstName

my_emps = sorted(my_emps, key=lambda emp : emp.firstName)
print_list(my_emps, 'Sort by firstName')


Find the below working application.

 

sort_with_custom_choice_2.py

class Employee:
    def __init__(self, id, firstName, lastName):
        self.id = id
        self.firstName = firstName
        self.lastName = lastName

    def __str__(self):
        return str(self.id)+"," +self.firstName + ","+self.lastName

def print_list(my_list, msg):
    print(msg)
    for item in my_list:
        print(item)
    print()

emp1 = Employee(1, "Krishna", "Gurram")
emp2 = Employee(21, "PTR", "Nayan")
emp3 = Employee(3, "Sankalp", "Dubey")

my_emps = [emp1, emp2, emp3]

print_list(my_emps, 'Actual list content')

my_emps.sort(key=lambda emp : emp.id)
print_list(my_emps, 'Sort by id')

my_emps = sorted(my_emps, key=lambda emp : emp.firstName)
print_list(my_emps, 'Sort by firstName')


Output

Actual list content
1,Krishna,Gurram
21,PTR,Nayan
3,Sankalp,Dubey

Sort by id
1,Krishna,Gurram
3,Sankalp,Dubey
21,PTR,Nayan

Sort by firstName
1,Krishna,Gurram
21,PTR,Nayan
3,Sankalp,Dubey


Previous                                                    Next                                                    Home

No comments:

Post a Comment