Open In App

Sort a Tuple of Custom Objects by Properties in Python

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given a tuple of custom objects and our task is to sort a tuple of custom objects by properties in Python and print the result. In this article, we will see how to sort a tuple of custom objects by properties in Python.

Example:

Input: CustomObject("Alice", 30), CustomObject("Bob", 25), CustomObject("Charlie", 35)
Output: Bob 25
Alice 30
Charlie 35
Explanation: Here, we have three custom objects which we sort by their age.

How to Sort a Tuple of Custom Objects by Properties in Python

Here, below are examples of how to sort a tuple of custom objects by Properties in Python:

  • Using Single Property
  • Using Multiple Properties
  • By Descending Order

Sorting a Tuple of Custom Objects by a Single Property

In this example, below code defines a custom class CustomObject with attributes name and age, creates a tuple of instances of this class, sorts the tuple based on the age property of each object, and finally prints the sorted tuple displaying the name and age of each object.

Python3
# Define the custom object class
class CustomObject:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Create a tuple of custom objects
custom_tuple = (CustomObject("Alice", 30),
                CustomObject("Bob", 25),
                CustomObject("Charlie", 35))

# Sort the tuple by the 'age' property
sorted_tuple = sorted(custom_tuple, key=lambda x: x.age)

# Print the sorted tuple
for obj in sorted_tuple:
    print(obj.name, obj.age)

Output
Bob 25
Alice 30
Charlie 35

Sorting a Tuple of Custom Objects by Multiple Properties

In this example, below code defines a custom class CustomObject with attributes name, age, and height, creates a tuple of instances of this class, sorts the tuple first by the age property and then by the height property of each object, and finally prints the sorted tuple displaying the result.

Python3
# Define the custom object class
class CustomObject:
    def __init__(self, name, age, height):
        self.name = name
        self.age = age
        self.height = height

# Create a tuple of custom objects
custom_tuple = (CustomObject("Alice", 30, 170),
                CustomObject("Bob", 25, 160),
                CustomObject("Charlie", 35, 175))

# Sort the tuple by the 'age' property first, then by the 'height' property
sorted_tuple = sorted(custom_tuple, key=lambda x: (x.age, x.height))

# Print the sorted tuple
for obj in sorted_tuple:
    print(obj.name, obj.age, obj.height)

Output
Bob 25 160
Alice 30 170
Charlie 35 175

Sorting a Tuple of Custom Objects in Descending Order

In this example, below code defines a custom class CustomObject with attributes name and score, creates a tuple of instances of this class, sorts the tuple based on the score property of each object in descending order, and finally prints the sorted tuple displaying the result.

Python3
# Define the custom object class
class CustomObject:
    def __init__(self, name, score):
        self.name = name
        self.score = score


# Create a tuple of custom objects
custom_tuple = (CustomObject("Alice", 85),
                CustomObject("Bob", 92),
                CustomObject("Charlie", 78))

# Sort the tuple by the 'score' property in descending order
sorted_tuple = sorted(custom_tuple, key=lambda x: x.score, reverse=True)

# Print the sorted tuple
for obj in sorted_tuple:
    print(obj.name, obj.score)

Output
Bob 92
Alice 85
Charlie 78

Conclusion

In conclusion , sorting a tuple of the custom objects by properties in Python is a straightforward task using the sorted() function with the custom key function. By providing a lambda function that extracts the desired property from the each object we can easily sort the tuple based on the different criteria. Whether it’s sorting by name, age or any other property this approach allows for the flexible and efficient sorting of the custom objects in Python.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads