|
1 | 1 | from taskwiki import constants |
2 | 2 | from taskwiki import util |
| 3 | +import re |
| 4 | + |
3 | 5 |
|
4 | 6 | class TaskSorter(object): |
5 | 7 | def __init__(self, cache, tasks, sortstring=None): |
@@ -110,16 +112,26 @@ def generic_compare(self, first, second, method): |
110 | 112 | return True if method == 'gt' else False |
111 | 113 |
|
112 | 114 | # Non-None values should respect reverse flags, use loop_method |
113 | | - if first_value < second_value: |
| 115 | + if self.natural_compare(first_value, second_value) == -1: |
114 | 116 | return True if loop_method == 'lt' else False |
115 | | - elif first_value > second_value: |
| 117 | + elif self.natural_compare(first_value, second_value) == 1: |
116 | 118 | return True if loop_method == 'gt' else False |
117 | 119 | else: |
118 | 120 | # Values are equal, move to next distinguisher |
119 | 121 | continue |
120 | 122 |
|
121 | 123 | return True if method == 'eq' else False |
122 | 124 |
|
| 125 | + def natural_compare(self, a, b): |
| 126 | + if not isinstance(a, str): |
| 127 | + return (a > b) - (a < b) |
| 128 | + |
| 129 | + convert = lambda text: int(text) if text.isdigit() else text.lower() |
| 130 | + alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+).*$', key)] |
| 131 | + |
| 132 | + return (alphanum_key(a) > alphanum_key(b)) - (alphanum_key(a) < alphanum_key(b)) |
| 133 | + |
| 134 | + |
123 | 135 | def lt(self, first, second): |
124 | 136 | return self.generic_compare(first, second, 'lt') |
125 | 137 |
|
|
0 commit comments