As a side note, here is a better alternative to implement the same sorting:
alist.sort(key=lambda x: x.foo)
Or alternatively:
import operator
alist.sort(key=operator.attrgetter('foo'))
Check out the Sorting How To, it is very useful.
More Related Contents:
- How do I sort a list of dictionaries by a value of the dictionary?
- Why does “return list.sort()” return None, not the list?
- Sorting list based on values from another list
- What is the difference between `sorted(list)` vs `list.sort()`?
- From list of integers, get number closest to a given value
- sort() and reverse() functions do not work
- Finding median of list in Python
- Why does ‘.sort()’ cause the list to be ‘None’ in Python? [duplicate]
- Sorting Python list based on the length of the string
- How to sort and remove duplicates from Python list? [duplicate]
- List sorting with multiple attributes and mixed order
- Sorting a dictionary with lists as values, according to an element from the list
- Sort multidimensional array based on 2nd element of the subarray
- Python – How to sort a list of lists by the fourth element in each list? [duplicate]
- Sort a list of lists with a custom compare function
- django – convert a list back to a queryset [duplicate]
- Python data structure sort list alphabetically
- Sort list of strings by a part of the string
- Efficient method to calculate the rank vector of a list in Python
- Python .sort() not working as expected
- How to sort a list of objects based on an attribute of the objects in descending order?
- How do I sort a list of objects based on an attribute of the objects?
- Sorting list according to corresponding values from a parallel list [duplicate]
- How to sort multidimensional array by column?
- How to sort a 2D list? [closed]
- How to sort a list of strings in reverse order without using reverse=True parameter?
- Python: How to sort a list of dictionaries by several values?
- Split a list of tuples into sub-lists of the same tuple field [duplicate]
- Nested lambda statements when sorting lists
- Rearranging list based on order of another list