This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients cykerway, serhiy.storchaka, steven.daprano, tim.peters, xtreak
Date 2018-10-17.13:20:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1539782455.89.0.788709270274.issue35010@psf.upfronthosting.co.za>
In-reply-to
Content
@Serhiy I just checked the docs to add an example and it's explained with an example at [0] :)


```

Sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved.

This wonderful property lets you build complex sorts in a series of sorting steps. For example, to sort the student data by descending grade and then ascending age, do the age sort first and then sort again using grade:

>>> s = sorted(student_objects, key=attrgetter('age'))     # sort on secondary key
>>> sorted(s, key=attrgetter('grade'), reverse=True)       # now sort on primary key, descending
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

The Timsort algorithm used in Python does multiple sorts efficiently because it can take advantage of any ordering already present in a dataset.

```

As noted TimSort makes this efficient by taking advantage of the sorting in the first run though it was not done as a single pass. If I am bench marking correctly in the attached file then for a list with 400 items using multiple pass sort is 4-5x faster than the suggested sorting in Python 3.7 and also more readable (though my lambda call is messy :)

Multiple pass sort :
3.871509724
Suggested sort :
17.237952677


[0] https://docs.python.org/3/howto/sorting.html#sort-stability-and-complex-sorts
History
Date User Action Args
2018-10-17 13:20:55xtreaksetrecipients: + xtreak, tim.peters, steven.daprano, serhiy.storchaka, cykerway
2018-10-17 13:20:55xtreaksetmessageid: <1539782455.89.0.788709270274.issue35010@psf.upfronthosting.co.za>
2018-10-17 13:20:55xtreaklinkissue35010 messages
2018-10-17 13:20:55xtreakcreate