New submission from Cyker Way <cyker...@gmail.com>:
The current `sorted` function is somewhat limited and doesn't cover a use case that frequently occurs in real applications: sort by a tuple of keys where each key can be in asc or desc order. For example, you may have a list of site configs where each of specify which user on which site gets how much quota. This data may be loaded from a SQL table where there are 3 columns: url, user, quota. Often you may want to sort by url, but when you want to check which users have been allocated most quota, you probably want to sort by quota. Even more likely, when you are sorting by quota, you still want to sort by url for those having the same quota. Unfortunately, current `sorted` function doesn't allow you to sort by desc quota and asc url at the same time, because the `reverse` parameter acts on the final result, regardless of columns. For numeric columns, there is a trick of using minus sign on the data. But this approach usually doesn't apply to other types of data. The general solution is to enhance the key function, allowing users to specify each column to be considered, with an asc/desc flag: keyspec = [ (itemgetter('url'), False), (itemgetter('user'), True), ] An example is worth 1k words. The full example is provided in the attachment. It's not a lot of code to write but making this feature builtin can save a lot of work since sorting a SQL table or other sheet data is quite common in real applications. ---------- components: Library (Lib) files: c.py messages: 327886 nosy: cykerway priority: normal severity: normal status: open title: sort by partially reversed key tuple type: enhancement versions: Python 3.8 Added file: https://bugs.python.org/file47873/c.py _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue35010> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com