Python docs mention that list sorting is stable: https://docs.python.org/2/library/stdtypes.html#index-29
In there it says, "Starting with Python 2.3, the sort() method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade)"
Same is mentioned on another link on Stackoverflow where some users have linked the same docs and repeated that the Python sort has a stable sort: Sorting a Python list by two fields
However, I don't fully understand this concept. In this example below, I had expected that the result from each print statement would be the same but that is not the case.
Sample code -
from operator import itemgetter
list1 = []
list1.append(('A','Z','50'))
list1.append(('A','Y','10'))
list1.append(('B','Z','10'))
list1.append(('B','Y','50'))
list1.sort(key = itemgetter(0,1,2))
print(list1)
list2 = []
list2.append(('A','Z','50'))
list2.append(('A','Y','10'))
list2.append(('B','Z','10'))
list2.append(('B','Y','50'))
list2.sort(key = itemgetter(0,1))
list2.sort(key = itemgetter(2))
print(list2)
The result I get is this -
I am wondering why am I not getting the same results?
You've got the sorts backward. You need to sort by itemgetter(2)
first, then sort by itemgetter(0, 1)
, to get the same result as sorting by itemgetter(0, 1, 2)
.
The terminology might be confusing. A sort where comparisons are done by X, breaking ties by Y, is often referred to as "sorting by X, then by Y", but it's completely different from performing one sort by X, then a second sort by Y.