To sort the list in place:

orig_list.sort(key=lambda x: x.count, reverse=True)

To return a new list, use sorted:

new_list = sorted(orig_list, key=lambda x: x.count, reverse=True)

Explanation:

  • key=lambda x: x.count sorts by count.
  • reverse=True sorts in descending order.

More on sorting by keys.

Answer from Kenan Banks on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how to sort a list of objects?
r/learnpython on Reddit: How to sort a list of objects?
March 27, 2021 -

So I want to sort a list of objects by a particular attribute that they have using the "sorted" function because the timsort algorithm that the "sorted" function has is super fast.

a list like this, having objects which have a certain attribute "a" which is an int

object_list = [object1, object2, object3]

for i in object_list:
    print(i.a)

output:

30

56

23

how do I sort this list by that attribute in ascending order using the "sorted" function?

thanks in advance!

Discussions

How to sort a list of objects?
Using a lambda function as the key is the most straight-forward, but using operator.attrgetter() tends to be slightly faster. So if that's a concern, you could use from operator import attrgetter sorted(object_list, key=attrgetter('a')) More on reddit.com
🌐 r/learnpython
8
3
March 27, 2021
Python - Sort list by object attribute [x-post via /r/learnpython]
You should use lamba particle, not lambda particles. I made the change on my end and it worked fine. So the line should read: self.particles.sort(key=lambda particle: particle.dist) More on reddit.com
🌐 r/learnprogramming
2
2
August 5, 2013
Setting list as a class attribute
An attribute, like a variable, is simply holding a reference to a Python object, not the object itself. The object referenced can be any Python object including a new empty list object, which is what [] creates. Thus, when you have self.cars = [] in an instance method of a class, when the instance is created and the corresponding method is run (by default in the case of __init__) either a new attribute will be created for the instance referenced by self and assigned to reference a new list object, or the reference in an existing attribute will be replaced by a reference to the new list object (this would be in a different method from __init__). EDIT: Sorry, keyboard problems, kept submitting before I pressed finished. More on reddit.com
🌐 r/learnpython
4
3
March 11, 2024
Unresolved attribute??

Maybe you used tile somewhere else in your code? The above works for me. But read pep8 for naming conventions, check out itertools.product and sets for some suggestions on making your code a little prettier.

More on reddit.com
🌐 r/learnpython
6
2
January 15, 2016
🌐
DEV Community
dev.to › edewajosh › python-how-to-sort-a-list-of-objects-based-on-attributes-of-objects-jaa
Python-How to sort a list of objects based on attribute(s) of objects? - DEV Community
August 4, 2022 - Now, how do we sort a list of objects given above based on one or more attributes of the object. We will begin by defining a class of that object can be created from. Since python does have operator module that offers us with itemgetter(), attrgetter(), and a methodcaller() that will be helpful ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-a-list-of-objects-by-multiple-attributes-in-python
Sort a list of objects by multiple attributes in Python - GeeksforGeeks
July 23, 2025 - Further, run the Python code using the following command in the terminal– ... Time complexity: O(n log n), where n is the number of elements in the list. Space complexity: O(n), as a new list is created to store the sorted elements, which takes up O(n) space. In this way, we use the sorted function with the itemgetter, rather than using the sorted function directly. The itemgetter gives the user the freedom to sort by multiple attributes.
🌐
Medium
medium.com › data-science › simple-sorting-of-a-list-of-objects-by-a-specific-property-using-python-dac907150394
Simple sorting of a list of objects by a specific property using Python | by Gibson Tang | TDS Archive | Medium
June 13, 2020 - So, by passing in the list of unsorted cars, and the anonymous function and the attribute to sort by, which is the name of the car. We will get the sorted list in result_list. This is a much better way of sorting, although the usage of the Lamba veers your Python code into the functional programming paradigm.
🌐
Quora
quora.com › How-do-I-sort-a-list-of-objects-based-on-an-attribute-of-the-objects-in-Python
How to sort a list of objects based on an attribute of the objects in Python - Quora
Python (programming langu... ... Sorting a list of objects by one of their attributes in Python is straightforward using list.sort() (in-place) or sorted() (returns new list).
Find elsewhere
🌐
FavTutor
favtutor.com › blogs › sort-list-of-objects-python
Sort a List of Objects in Python | FavTutor
January 24, 2022 - By in-place sort means that the changes are done to the original list and no new auxiliary list is created to fulfill the purpose. The signature of this method is : ... Key: This is a single argument function, that extracts a comparison key from each list object. In other words, this is a function to get the attribute according to which sorting is to be done.
🌐
CoenRaets
jsonviewer.ai › python-sort-list-of-objects-by-properties
Python sort list of objects by Properties[with Examples] - JSON Viewer
July 4, 2023 - Next, we use the sorted() function with the key parameter set to id to sort the list of Student objects by their unique ids. This tells the sorted() function to use the id function as the sorting key for the list of Student objects. Finally, we print the sorted list of students by iterating ...
🌐
Python documentation
docs.python.org › 3 › howto › sorting.html
Sorting Techniques — Python 3.14.3 documentation
1 month ago - >>> class Student: ... def __init__(self, name, grade, age): ... self.name = name ... self.grade = grade ... self.age = age ... def __repr__(self): ... return repr((self.name, self.grade, self.age)) >>> student_objects = [ ... Student('john', 'A', 15), ... Student('jane', 'B', 12), ... Student('dave', 'B', 10), ... ] >>> sorted(student_objects, key=lambda student: student.age) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] Objects with named attributes can be made by a regular class as shown above, or they can be instances of dataclass or a named tuple.
🌐
W3docs
w3docs.com › python
How to sort a list of objects based on an attribute of the objects?
To sort in descending order, you ... reverse=True) ... Alternatively, you can use the sorted() function in python that takes in the list and a key function and returns a sorted list....
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch02s07.html
Sorting a List of Objects by an Attribute of the Objects - Python Cookbook [Book]
July 19, 2002 - In this case, the obvious approach is concise, but quite slow: def sort_by_attr_slow(seq, attr): def cmp_by_attr(x, y, attr=attr): return cmp(getattr(x, attr), getattr(y, attr)) seq.sort(cmp_by_attr)
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
Quora
quora.com › What-is-the-most-efficient-way-to-sort-a-list-of-objects-in-Python-by-multiple-attributes
What is the most efficient way to sort a list of objects in Python by multiple attributes? - Quora
Answer: Python’s default sorting function is hard to beat in terms of speed by anything you code yourself in Python, as it’s implemented in C. If your objects are instances of the same class you designed Python will use the __lt__ method of the object class to compare the objects, so if ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › sorting-objects-of-user-defined-class-in-python
Sorting Objects of User Defined Class in Python - GeeksforGeeks
July 23, 2025 - Explanation: sorted() sorts the list based on the b attribute of each object, using attrgetter('b') as the key argument.
🌐
Agnostic Development
agnosticdev.com › content › how-sort-objects-custom-property-python
How to Sort Objects by a Custom Property in Python | Agnostic Development
November 23, 2018 - In the real world you will probably encounter a situation where you will need to sort a data structure or a list by property. So I took my unsorted objects and added them to a list - out of order. From here I used the Python sort method to sort my list by date property in descending order using the optional key and reverse parameters.
🌐
Towards Data Science
towardsdatascience.com › home › latest › sorting objects in python
Sorting Objects in Python | Towards Data Science
January 21, 2025 - To summarize, in this post we discussed how to sort objects in python. First we showed how to sort objects corresponding to instances of the ‘AppleMusicUser’ class according to the attribute ‘apple_id’ using the sorted method and a lambda function. Next, we showed how to perform the same sorting task by using the ‘attrgetter’ method in the operator module.
🌐
Delft Stack
delftstack.com › home › howto › python › python sort list of objects
How to Sort List of Objects in Python | Delft Stack
February 2, 2024 - attrgetter() is imported from the operator module and is utilized to return a callable object that fetches the attribute from its operand. The following code uses the list.sort() method and operator.attrgetter() to sort the list of objects in Python.
🌐
pythontutorials
pythontutorials.net › blog › how-do-i-sort-a-list-of-objects-based-on-an-attribute-of-the-objects
How to Sort a List of Objects by Attribute in Python: Sorting by Count in Descending Order — pythontutorials.net
If two objects have the same count, Python’s sort is stable (preserves the original order of equal elements). For example, apple and date both have count=5 in our sample data—their order in the sorted list matches their order in the original list. To override this, add a secondary sort key (e.g., name in ascending order): # Sort by count (descending), then name (ascending) for ties sorted_ties = sorted( fruits, key=lambda x: (-x.count, x.name), # Negative count for descending reverse=False # No need for reverse=True with negative count ) print("Sorted with tiebreaker (name):", sorted_ties)
🌐
Goom Articles
goom.ai › articles › python-sort-list-of-objects-by-properties
Python sort list of objects by Properties[with Examples] - Goom Articles
July 4, 2023 - Next, we use the sorted() function with the key parameter set to id to sort the list of Student objects by their unique ids. This tells the sorted() function to use the id function as the sorting key for the list of Student objects. Finally, we print the sorted list of students by iterating ...