From d39313638c864572cf289bfa48afbf0aa8f53614 Mon Sep 17 00:00:00 2001 From: pfackeldey Date: Fri, 22 Nov 2024 13:10:12 +0100 Subject: [PATCH] fix sorting of record arrays made of tuples --- src/awkward/contents/recordarray.py | 2 +- tests/test_1316_sort_recordarrays.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/test_1316_sort_recordarrays.py diff --git a/src/awkward/contents/recordarray.py b/src/awkward/contents/recordarray.py index 4aafcfd6b2..9b47964225 100644 --- a/src/awkward/contents/recordarray.py +++ b/src/awkward/contents/recordarray.py @@ -829,7 +829,7 @@ def _argsort_next( raise NotImplementedError def _sort_next(self, negaxis, starts, parents, outlength, ascending, stable): - if self._fields is None or len(self._fields) == 0: + if len(self.fields) == 0: return ak.contents.NumpyArray( self._backend.nplike.instance().empty(0, dtype=np.int64), parameters=None, diff --git a/tests/test_1316_sort_recordarrays.py b/tests/test_1316_sort_recordarrays.py new file mode 100644 index 0000000000..f5173e4186 --- /dev/null +++ b/tests/test_1316_sort_recordarrays.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +import numpy as np + +import awkward as ak + + +def test_sort_record(): + a = ak.contents.NumpyArray(np.array([50, 500, 100, 1, 200, 1000])) + + record = ak.Array(ak.contents.RecordArray([a, a[::-1]], ["a", "b"])) + + assert ak.to_list(ak.sort(record)["a"]) == [1, 50, 100, 200, 500, 1000] + assert ak.to_list(ak.sort(record)["b"]) == [1, 50, 100, 200, 500, 1000] + + +def test_sort_record_tuple(): + a = ak.contents.NumpyArray(np.array([50, 500, 100, 1, 200, 1000])) + + record = ak.Array(ak.contents.RecordArray([a, a[::-1]], None)) + + assert ak.to_list(ak.sort(record)["0"]) == [1, 50, 100, 200, 500, 1000] + assert ak.to_list(ak.sort(record)["1"]) == [1, 50, 100, 200, 500, 1000]