Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/safeds/data/tabular/containers/_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,22 @@ def get_column_type(self, column_name: str) -> ColumnType:

def sort_columns(
self,
comparator: Callable[[tuple, tuple], int] = lambda col1, col2: (col1[0] > col2[0]) - (col1[0] < col2[0]),
comparator: Callable[[str, Any, str, Any], int] = lambda name_1, _value_1, name_2, _value_2: (
name_1[0] > name_2[0]
)
- (name_1[0] < name_2[0]),
) -> Row:
"""
Sort the columns of a `Row` with the given comparator and return a new `Row`.

The original row is not modified. The comparator is a function that takes two tuples of (ColumnName,
Value) `col1` and `col2` and returns an integer:
The original row is not modified. The comparator is a function with four parameters:

* `name_1` is the name of the first column.
* `value_1` is the value of the first column.
* `name_2` is the name of the second column.
* `value_2` is the value of the second column.

It should return an integer, indicating the desired order of the columns:

* If `col1` should be ordered before `col2`, the function should return a negative number.
* If `col1` should be ordered after `col2`, the function should return a positive number.
Expand All @@ -500,7 +509,16 @@ def sort_columns(
new_row:
A new row with sorted columns.
"""
sorted_row_dict = dict(sorted(self.to_dict().items(), key=functools.cmp_to_key(comparator)))

def cmp(column_1: tuple[str, Any], column_2: tuple[str, Any]) -> int:
return comparator(column_1[0], column_1[1], column_2[0], column_2[1])

sorted_row_dict = dict(
sorted(
self.to_dict().items(),
key=functools.cmp_to_key(cmp),
),
)
return Row.from_dict(sorted_row_dict)

# ------------------------------------------------------------------------------------------------------------------
Expand Down
11 changes: 8 additions & 3 deletions tests/safeds/data/tabular/containers/test_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,12 @@ class TestSortColumns:
[
(
Row({"b": 1, "a": 2}),
lambda col1, col2: (col1[0] > col2[0]) - (col1[0] < col2[0]),
lambda name_1, _value_1, name_2, _value_2: (name_1 > name_2) - (name_1 < name_2),
Row({"a": 2, "b": 1}),
),
(
Row({"a": 2, "b": 1}),
lambda col1, col2: (col2[0] > col1[0]) - (col2[0] < col1[0]),
lambda name_1, _value_1, name_2, _value_2: (name_2 > name_1) - (name_2 < name_1),
Row({"b": 1, "a": 2}),
),
(Row(), lambda col1, col2: (col1[0] > col2[0]) - (col1[0] < col2[0]), Row()),
Expand All @@ -569,7 +569,12 @@ class TestSortColumns:
"empty rows",
],
)
def test_should_sort_columns(self, row: Row, comparator: Callable[[tuple, tuple], int], expected: Row) -> None:
def test_should_sort_columns(
self,
row: Row,
comparator: Callable[[str, Any, str, Any], int],
expected: Row,
) -> None:
row = row.sort_columns(comparator)
assert row == expected

Expand Down