Skip to content

Commit

Permalink
[sort api] add Sheet.ordering property as list[tuple[Column,bool]] #2295
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Feb 23, 2024
1 parent ffcc2db commit ad494fe
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions visidata/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ def __lt__(self, other):
return other.obj < self.obj


@Sheet.api
def sortkey(self, r):

@Sheet.cached_property
def ordering(sheet) -> 'list[tuple[Column, bool]]':
ret = []
for col, reverse in self._ordering:
if isinstance(col, str):
col = self.column(col)
ret.append((col, reverse))
return ret


@Sheet.api
def sortkey(sheet, r, ordering:'list[tuple[Column, bool]]'=[]):
ret = []
for col, reverse in (ordering or self.ordering):
val = col.getTypedValue(r)
ret.append(Reversor(val) if reverse else val)


return ret


@Sheet.api
@asyncthread
def sort(self):
Expand All @@ -55,8 +64,13 @@ def sort(self):
return
try:
with Progress(gerund='sorting', total=self.nRows) as prog:
ordering = self.ordering
def _sortkey(r):
prog.addProgress(1)
return self.sortkey(r, ordering=ordering)

# must not reassign self.rows: use .sort() instead of sorted()
self.rows.sort(key=lambda r,self=self,prog=prog: (prog.addProgress(1), self.sortkey(r))[1])
self.rows.sort(key=_sortkey)
except TypeError as e:
vd.warning('sort incomplete due to TypeError; change column type')
vd.exceptionCaught(e, status=False)
Expand Down

0 comments on commit ad494fe

Please sign in to comment.