diff --git a/src/pydash/chaining/all_funcs.pyi b/src/pydash/chaining/all_funcs.pyi index 308171f2..6e4c0ad4 100644 --- a/src/pydash/chaining/all_funcs.pyi +++ b/src/pydash/chaining/all_funcs.pyi @@ -1176,7 +1176,7 @@ class AllFuncs: @t.overload def order_by( self: "Chain[t.Mapping[t.Any, T2]]", - keys: t.Iterable[str], + keys: t.Iterable[t.Union[str, int]], orders: None = None, reverse: bool = False, ) -> "Chain[t.List[T2]]": ... @@ -1190,7 +1190,7 @@ class AllFuncs: @t.overload def order_by( self: "Chain[t.Iterable[T]]", - keys: t.Iterable[str], + keys: t.Iterable[t.Union[str, int]], orders: None = None, reverse: bool = False, ) -> "Chain[t.List[T]]": ... diff --git a/src/pydash/collections.py b/src/pydash/collections.py index fc5bdb67..0d2947bb 100644 --- a/src/pydash/collections.py +++ b/src/pydash/collections.py @@ -1138,7 +1138,7 @@ def order_by( @t.overload def order_by( collection: t.Mapping[t.Any, T2], - keys: t.Iterable[str], + keys: t.Iterable[t.Union[str, int]], orders: None = None, reverse: bool = False, ) -> t.List[T2]: ... @@ -1156,7 +1156,7 @@ def order_by( @t.overload def order_by( collection: t.Iterable[T], - keys: t.Iterable[str], + keys: t.Iterable[t.Union[str, int]], orders: None = None, reverse: bool = False, ) -> t.List[T]: ... @@ -1171,8 +1171,9 @@ def order_by(collection, keys, orders=None, reverse=False): Args: collection: Collection to iterate over. - keys: List of keys to sort by. By default, keys will be sorted in ascending order. To - sort a key in descending order, prepend a ``"-"`` to the key name. For example, to sort + keys: List of string keys or integer keys/indexes to sort by. By default, keys will be + sorted in ascending order. To sort a string key in descending order, prepend a + ``"-"`` to the key name. For example, to sort the key value for ``"name"`` in descending order, use ``"-name"``. orders: List of boolean sort orders to apply for each key. ``True`` corresponds to ascending order while ``False`` is descending. Defaults to ``None``. @@ -1200,6 +1201,8 @@ def order_by(collection, keys, orders=None, reverse=False): >>> assert results == [{'a': 3, 'b': 2},\ {'a': 2, 'b': 1},\ {'a': 1, 'b': 3}] + >>> order_by([[3], [1], [2]], [0]) + [[1], [2], [3]] .. versionadded:: 3.0.0 @@ -1236,7 +1239,7 @@ def order_by(collection, keys, orders=None, reverse=False): comparers.append((pyd.property_(key), order)) else: for key in keys: - if key.startswith("-"): + if isinstance(key, str) and key.startswith("-"): order = -1 key = key[1:] else: diff --git a/tests/pytest_mypy_testing/test_collections.py b/tests/pytest_mypy_testing/test_collections.py index c0fb678b..71653baa 100644 --- a/tests/pytest_mypy_testing/test_collections.py +++ b/tests/pytest_mypy_testing/test_collections.py @@ -131,6 +131,16 @@ def test_mypy_order_by() -> None: reveal_type(_.order_by([{"a": 2, "b": 1}, {"a": 3, "b": 2}, {"a": 1, "b": 3}], ["a", "b"], [False, True])) # R: builtins.list[builtins.dict[builtins.str, builtins.int]] +@pytest.mark.mypy_testing +def test_mypy_order_by_numeric_keys() -> None: + rows = [[2], [1]] + mapping = {"second": [2], "first": [1]} + assert_type(_.order_by(rows, [0]), t.List[t.List[int]]) + assert_type(_.order_by(mapping, [0]), t.List[t.List[int]]) + assert_type(_.chain(rows).order_by([0]).value(), t.List[t.List[int]]) + assert_type(_.chain(mapping).order_by([0]).value(), t.List[t.List[int]]) + + @pytest.mark.mypy_testing def test_mypy_partition() -> None: reveal_type(_.partition([1, 2, 3, 4], lambda x: x >= 3)) # R: builtins.list[builtins.list[builtins.int]] diff --git a/tests/test_collections.py b/tests/test_collections.py index ea569d52..b9ecfdd7 100644 --- a/tests/test_collections.py +++ b/tests/test_collections.py @@ -814,6 +814,28 @@ def test_order_by_accepts_iterable_orders(): assert _.order_by(items, ["a", "b"], iter([False])) == _.order_by(items, ["a", "b"], [False]) +@parametrize("orders", [None, [], iter([]), False, [True]]) +def test_order_by_numeric_keys_with_default_directions(orders): + rows = [[2, "second"], [1, "first"], [2, "third"]] + expected = [rows[1], rows[0], rows[2]] + assert _.order_by(rows, [0], orders) == expected + assert rows == [[2, "second"], [1, "first"], [2, "third"]] + + +def test_order_by_numeric_keys_mapping_and_chain(): + rows = [{0: 2, "name": "a"}, {0: 1, "name": "b"}, {0: 2, "name": "c"}] + expected = [rows[1], rows[2], rows[0]] + assert _.order_by(dict(enumerate(rows)), [0, "-name"]) == expected + assert _.chain(rows).order_by([0, "-name"]).value() == expected + + +def test_order_by_numeric_keys_explicit_and_reverse_directions(): + rows = [[2], [1], [3]] + assert _.order_by(rows, [0], [False]) == [[3], [2], [1]] + assert _.order_by(rows, [0], True) == [[3], [2], [1]] + assert _.order_by(rows, [0], reverse=True) == [[3], [2], [1]] + + @parametrize( "case,expected", [