[New-bugs-announce] [issue35956] Sort documentation could be improved for complex sorting

fabrice salvaire report at bugs.python.org
Sun Feb 10 11:33:39 EST 2019


New submission from fabrice salvaire <fabrice.salvaire at orange.fr>:

I just implemented Graham Scan algorithm in Python 3 and have to read carefully the sort documentation.  Notice this is a good algorithm for a large audience language like Python.

Since Python 3, the old order function cmp is depicted as an old way to proceed.

But some sorting procedure require complex order like this

    def sort_by_y(p0, p1):
        return p0.x - p1.x if (p0.y == p1.y) else p0.y - p1.y
    sorted(points, key=cmp_to_key(sort_by_y))

which is less natural to implement than

    def sort_by_y(p0, p1):
        return p0.x < p1.x if (p0.y == p1.y) else p0.y < p1.y
    sorted(points, cmp=sort_by_y)

Since Python 3 we should do this

    points.sort(key=attrgetter('x'))
    points.sort(key=attrgetter('y'))

But we must take care to the chaining order !!! Here we must sort first on x then on y.

I think the documentation could explain much better how to perform complex sort and the performance of the Python sort algorithm.  Is the old way faster than the new one ???  What about short and large array ???  What happen when we sort a zillion of short array ???

----------
assignee: docs at python
components: Documentation
messages: 335163
nosy: FabriceSalvaire, docs at python
priority: normal
severity: normal
status: open
title: Sort documentation could be improved for complex sorting
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35956>
_______________________________________


More information about the New-bugs-announce mailing list