DeveloperBreeze

Sort List of Objects by Attribute in Python

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Create instances of Person
people = [Person('Alice', 30), Person('Bob', 25), Person('Charlie', 35)]

# Sort people by age using a lambda function as the key
sorted_people = sorted(people, key=lambda x: x.age)

Continue Reading

Discover more amazing content handpicked just for you

Code
javascript

Sorting an Array in JavaScript

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Sorting an Array

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Sorting a Dictionary by Values

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Sort a List

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!