Allow for ascending or descending sort order.

This commit is contained in:
Mike Auty
2017-08-01 02:41:55 +01:00
parent 5e9b2306d4
commit 80508c8711
2 changed files with 6 additions and 1 deletions
@@ -27,6 +27,8 @@ class Renderer(validity.ValidityRoutines, metaclass = ABCMeta):
class ColumnSortKey(metaclass = ABCMeta):
ascending = True
@abstractmethod
def key(self, values):
"""The key function passed as a sort key to the TreeGrid's visit function"""
+4 -1
View File
@@ -255,13 +255,16 @@ class TreeGrid(interfaces.renderers.TreeGrid):
accumulator = function(n, accumulator)
if sort_key is not None:
children = sorted(children, key = lambda x: sort_key(x[0].values))
if not sort_key.ascending:
children = reversed(children)
accumulator = self._visit(children, function, accumulator, sort_key)
return accumulator
class ColumnSortKey(interfaces.renderers.ColumnSortKey):
def __init__(self, treegrid, column_name):
def __init__(self, treegrid, column_name, ascending = True):
self._index = None
self.ascending = ascending
for i in treegrid.columns:
if i.name.lower() == column_name.lower():
self._index = i.index