Commit 1c11a56f authored by Bram Schoenmakers's avatar Bram Schoenmakers

Move sort algorithm outside class

parent 97b83c84
...@@ -71,6 +71,16 @@ def get_field_function(p_field): ...@@ -71,6 +71,16 @@ def get_field_function(p_field):
return result return result
def _apply_sort_functions(p_todos, p_functions):
sorted_todos = p_todos
for function, order in reversed(p_functions):
sorted_todos = sorted(sorted_todos, key=function,
reverse=(order == 'desc'))
return sorted_todos
class Sorter(object): class Sorter(object):
""" """
This class sorts a todo list. This class sorts a todo list.
...@@ -109,21 +119,14 @@ class Sorter(object): ...@@ -109,21 +119,14 @@ class Sorter(object):
sort operation is done first, relying on the stability of the sorted() sort operation is done first, relying on the stability of the sorted()
function. function.
""" """
sorted_todos = p_todos return _apply_sort_functions(p_todos, self.sortfunctions)
for function, order in reversed(self.sortfunctions):
sorted_todos = sorted(sorted_todos, key=function,
reverse=(order == 'desc'))
return sorted_todos
def group(self, p_todos): def group(self, p_todos):
""" """
Groups the todos according to the given group string. Assumes that the Groups the todos according to the given group string.
given todos have already been sorted with self.sort().
""" """
# preorder todos for the group sort # preorder todos for the group sort
for function, _ in self.groupfunctions: p_todos = _apply_sort_functions(p_todos, self.groupfunctions)
p_todos = sorted(p_todos, key=function)
# initialize result with a single group # initialize result with a single group
result = OrderedDict([((), p_todos)]) result = OrderedDict([((), p_todos)])
......
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