diff --git a/redis/commands/core.py b/redis/commands/core.py index 8c259792cd..3db4321151 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2224,6 +2224,39 @@ def sort( options = {"groups": len(get) if groups else None} return self.execute_command("SORT", *pieces, **options) + def sort_ro( + self, + key: str, + start: Optional[int] = None, + num: Optional[int] = None, + by: Optional[str] = None, + get: Optional[List[str]] = None, + desc: bool = False, + alpha: bool = False, + ) -> list: + """ + Returns the elements contained in the list, set or sorted set at key. + (read-only variant of the SORT command) + + ``start`` and ``num`` allow for paging through the sorted data + + ``by`` allows using an external key to weight and sort the items. + Use an "*" to indicate where in the key the item value is located + + ``get`` allows for returning items from external keys rather than the + sorted data itself. Use an "*" to indicate where in the key + the item value is located + + ``desc`` allows for reversing the sort + + ``alpha`` allows for sorting lexicographically rather than numerically + + For more information check https://redis.io/commands/sort_ro + """ + return self.sort( + key, start=start, num=num, by=by, get=get, desc=desc, alpha=alpha + ) + class ScanCommands: """ diff --git a/tests/test_commands.py b/tests/test_commands.py index c8f7ddc3f4..8ff9bec97e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2700,6 +2700,16 @@ def test_sort_all_options(self, r): assert num == 4 assert r.lrange("sorted", 0, 10) == [b"vodka", b"milk", b"gin", b"apple juice"] + @skip_if_server_version_lt("7.0.0") + def test_sort_ro(self, r): + r["score:1"] = 8 + r["score:2"] = 3 + r["score:3"] = 5 + r.rpush("a", "3", "2", "1") + assert r.sort_ro("a", by="score:*") == [b"2", b"3", b"1"] + r.rpush("b", "2", "3", "1") + assert r.sort_ro("b", desc=True) == [b"3", b"2", b"1"] + def test_sort_issue_924(self, r): # Tests for issue https://github.com/andymccurdy/redis-py/issues/924 r.execute_command("SADD", "issue#924", 1)