Commit 97b83c84 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Do not sort all todos when the items will be grouped

When grouping takes place, don't sort the full list of todo items first
and then group. Because the todos in the group won't necessarily match
the given sort expression, for those items which match multiple groups.

These are the steps instead:

1. Get all todo items
2. Filter them
3. Pre-order them according to the group expression
4. Split in groups using groupby()
5. Then sort the individual groups according to the sort expression
parent f423ed4e
...@@ -98,7 +98,7 @@ class Sorter(object): ...@@ -98,7 +98,7 @@ class Sorter(object):
def __init__(self, p_sortstring="desc:priority", p_groupstring=""): def __init__(self, p_sortstring="desc:priority", p_groupstring=""):
self.groupfunctions = self._parse(p_groupstring) if p_groupstring else [] self.groupfunctions = self._parse(p_groupstring) if p_groupstring else []
self.sortfunctions = self._parse(p_groupstring + ',' + p_sortstring) self.sortfunctions = self._parse(p_sortstring)
def sort(self, p_todos): def sort(self, p_todos):
""" """
...@@ -121,6 +121,11 @@ class Sorter(object): ...@@ -121,6 +121,11 @@ class Sorter(object):
Groups the todos according to the given group string. Assumes that the Groups the todos according to the given group string. Assumes that the
given todos have already been sorted with self.sort(). given todos have already been sorted with self.sort().
""" """
# preorder todos for the group sort
for function, _ in self.groupfunctions:
p_todos = sorted(p_todos, key=function)
# initialize result with a single group
result = OrderedDict([((), p_todos)]) result = OrderedDict([((), p_todos)])
for function, _ in self.groupfunctions: for function, _ in self.groupfunctions:
...@@ -141,6 +146,10 @@ class Sorter(object): ...@@ -141,6 +146,10 @@ class Sorter(object):
else: else:
result[newkey] = newgroup result[newkey] = newgroup
# sort all groups
for key, group in result.items():
result[key] = self.sort(group)
return result return result
def _parse(self, p_string): def _parse(self, p_string):
......
...@@ -29,17 +29,22 @@ class View(object): ...@@ -29,17 +29,22 @@ class View(object):
self._sorter = p_sorter self._sorter = p_sorter
self._filters = p_filters self._filters = p_filters
@property def _apply_filters(self, p_todos):
def todos(self): """ Applies the filters to the list of todo items. """
""" Returns a sorted and filtered list of todos in this view. """ result = p_todos
result = self._sorter.sort(self.todolist.todos())
for _filter in self._filters: for _filter in self._filters:
result = _filter.filter(result) result = _filter.filter(result)
return result return result
@property
def todos(self):
""" Returns a sorted and filtered list of todos in this view. """
result = self._sorter.sort(self.todolist.todos())
return self._apply_filters(result)
@property @property
def groups(self): def groups(self):
todos = self.todos result = self._apply_filters(self.todolist.todos())
return self._sorter.group(self.todos) return self._sorter.group(result)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment