mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Sort method
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
import functools
|
import functools
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from itertools import chain, zip_longest
|
from itertools import chain, zip_longest
|
||||||
|
from operator import itemgetter
|
||||||
from typing import (
|
from typing import (
|
||||||
ClassVar,
|
ClassVar,
|
||||||
Generic,
|
Generic,
|
||||||
@@ -11,6 +12,7 @@ from typing import (
|
|||||||
cast,
|
cast,
|
||||||
NamedTuple,
|
NamedTuple,
|
||||||
Callable,
|
Callable,
|
||||||
|
Sequence,
|
||||||
)
|
)
|
||||||
|
|
||||||
import rich.repr
|
import rich.repr
|
||||||
@@ -1107,6 +1109,21 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
|||||||
)
|
)
|
||||||
return Spacing(top, 0, 0, left)
|
return Spacing(top, 0, 0, left)
|
||||||
|
|
||||||
|
def sort(
|
||||||
|
self,
|
||||||
|
column: str | ColumnKey | Sequence[str] | Sequence[ColumnKey],
|
||||||
|
reverse: bool = False,
|
||||||
|
) -> None:
|
||||||
|
if isinstance(column, (str, ColumnKey)):
|
||||||
|
column = (column,)
|
||||||
|
indices = [self._column_locations.get(key) for key in column]
|
||||||
|
ordered_keys = sorted(self.rows, key=itemgetter(*indices), reverse=reverse)
|
||||||
|
self._row_locations = TwoWayDict(
|
||||||
|
{key: new_index for new_index, key in enumerate(ordered_keys)}
|
||||||
|
)
|
||||||
|
self._update_count += 1
|
||||||
|
self.refresh()
|
||||||
|
|
||||||
def sort_columns(
|
def sort_columns(
|
||||||
self, key: Callable[[ColumnKey | str], str] = None, reverse: bool = False
|
self, key: Callable[[ColumnKey | str], str] = None, reverse: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user