Python Example To Sort Employee By Multiple Properties

Here’s the Python example that demonstrates sorting employees by multiple properties we explained in depth below :

class Employee:
    def __init__(self, employee_id, name, address, pincode):
        self.employee_id = employee_id
        self.name = name
        self.address = address
        self.pincode = pincode

    def __str__(self):
        return f"Employee{{employee_id={self.employee_id}, name='{self.name}', address='{self.address}', pincode='{self.pincode}'}}"

employees = [
    Employee(2, "John", "123 Main St", "10001"),
    Employee(1, "Jane", "456 Elm St", "20002"),
    Employee(4, "Adam", "789 Oak St", "30003"),
    Employee(3, "Eva", "987 Maple St", "40004")
]

# Sort employees by employee_id
sorted_by_employee_id = sorted(employees, key=lambda emp: emp.employee_id)
print("Sorted by employee_id:")
print(*sorted_by_employee_id, sep="\n")
print()

# Sort employees by name
sorted_by_name = sorted(employees, key=lambda emp: emp.name)
print("Sorted by name:")
print(*sorted_by_name, sep="\n")
print()

# Sort employees by address
sorted_by_address = sorted(employees, key=lambda emp: emp.address)
print("Sorted by address:")
print(*sorted_by_address, sep="\n")
print()

# Sort employees by pincode
sorted_by_pincode = sorted(employees, key=lambda emp: emp.pincode)
print("Sorted by pincode:")
print(*sorted_by_pincode, sep="\n")
print()

# Sort employees by employee_id, name, and pincode
sorted_by_multiple_props = sorted(employees, key=lambda emp: (emp.employee_id, emp.name, emp.pincode))
print("Sorted by employee_id, name, and pincode:")
print(*sorted_by_multiple_props, sep="\n")

Output:

Sorted by employee_id:
Employee{employee_id=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employee_id=2, name='John', address='123 Main St', pincode='10001'}
Employee{employee_id=3, name='Eva', address='987 Maple St', pincode='40004'}
Employee{employee_id=4, name='Adam', address='789 Oak St', pincode='30003'}

Sorted by name:
Employee{employee_id=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employee_id=3, name='Eva', address='987 Maple St', pincode='40004'}
Employee{employee_id=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employee_id=2, name='John', address='123 Main St', pincode='10001'}

Sorted by address:
Employee{employee_id=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employee_id=3, name='Eva', address='987 Maple St', pincode='40004'}
Employee{employee_id=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employee_id=2, name='John', address='123 Main St', pincode='10001'}

Sorted by pincode:
Employee{employee_id=2, name='John', address='123 Main St', pincode='10001'}
Employee{employee_id=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employee_id=4, name='Adam',

 address='789 Oak St', pincode='30003'}
Employee{employee_id=3, name='Eva', address='987 Maple St', pincode='40004'}

Sorted by employee_id, name, and pincode:
Employee{employee_id=1, name='Jane', address='456 Elm St', pincode='20002'}
Employee{employee_id=2, name='John', address='123 Main St', pincode='10001'}
Employee{employee_id=4, name='Adam', address='789 Oak St', pincode='30003'}
Employee{employee_id=3, name='Eva', address='987 Maple St', pincode='40004'}

In the Python example, we define a Employee class with its properties and a __str__ method for string representation. We create a list of Employee objects and demonstrate how to sort them based on different properties using the sorted function and lambda expressions as key functions. Finally, we show an example of sorting by multiple properties using a tuple as the key function.

Explanation of Above Example :

Let’s go through the Python example in more detail:

class Employee:
    def __init__(self, employee_id, name, address, pincode):
        self.employee_id = employee_id
        self.name = name
        self.address = address
        self.pincode = pincode

    def __str__(self):
        return f"Employee{{employee_id={self.employee_id}, name='{self.name}', address='{self.address}', pincode='{self.pincode}'}}"

In this part, we define the Employee class. It has four properties: employee_id, name, address, and pincode. The __init__ method is the constructor that initializes the object with these properties. The __str__ method is overridden to provide a string representation of the Employee object when printed.

employees = [
    Employee(2, "John", "123 Main St", "10001"),
    Employee(1, "Jane", "456 Elm St", "20002"),
    Employee(4, "Adam", "789 Oak St", "30003"),
    Employee(3, "Eva", "987 Maple St", "40004")
]

In this part, we create a list called employees and populate it with Employee objects. Each Employee object is initialized with specific values for the properties.

sorted_by_employee_id = sorted(employees, key=lambda emp: emp.employee_id)
print("Sorted by employee_id:")
print(*sorted_by_employee_id, sep="\n")
print()

Here, we use the sorted function to sort the employees list based on the employee_id property. The key parameter is set to a lambda function that extracts the employee_id from each Employee object. The resulting sorted list is stored in the sorted_by_employee_id variable and then printed.

Similarly, we repeat this process for sorting by other properties: name, address, and pincode. We create separate variables (sorted_by_name, sorted_by_address, and sorted_by_pincode) to store the sorted lists and print them accordingly.

sorted_by_multiple_props = sorted(employees, key=lambda emp: (emp.employee_id, emp.name, emp.pincode))
print("Sorted by employee_id, name, and pincode:")
print(*sorted_by_multiple_props, sep="\n")

Finally, we demonstrate sorting by multiple properties. In this case, we use a tuple as the key parameter in the sorted function. The lambda function returns a tuple containing the values of employee_id, name, and pincode for each Employee object. The sorting is done in a hierarchical manner, first by employee_id, then by name, and finally by pincode. The resulting sorted list is stored in the sorted_by_multiple_props variable and printed.

Overall, the Python example demonstrates how to define a class, create objects, and sort them using the sorted function with lambda expressions as key functions. It showcases sorting by individual properties as well as sorting by multiple properties in a hierarchical manner.

Happy Learning..

Leave a Reply

Your email address will not be published. Required fields are marked *