Created
July 24, 2024 07:08
-
-
Save Anexen/797f4aa7de5d78bab9b1fc9f0e1a3a10 to your computer and use it in GitHub Desktop.
SQL-like sorting for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Desc: | |
| __slots__ = ("value",) | |
| def __init__(self, value): | |
| self.value = value | |
| def __lt__(self, other): | |
| return self.value > other.value | |
| data = [ | |
| {"x": 2, "y": "a"}, | |
| {"x": 2, "y": "b"}, | |
| {"x": 1, "y": "b"}, | |
| ] | |
| sorted(data, key=lambda x: (x["x"], Desc(x["y"]))) | |
| sorted(data, key=lambda x: (Desc(x["x"]), x["y"])) | |
| sorted(data, key=lambda x: (Desc(x["x"]), Desc(x["y"]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment