|
| 1 | +## `multisort` - NoneType Safe Multi Column Sorting |
| 2 | + |
| 3 | +Simplified multi-column sorting of lists of tuples, dicts, lists or objects that are NoneType safe. |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +``` |
| 8 | +python3 -m pip install multisort |
| 9 | +``` |
| 10 | + |
| 11 | +### Dependencies |
| 12 | +None |
| 13 | + |
| 14 | +### Performance |
| 15 | +Average over 10 iterations with 500 rows. |
| 16 | +Test | Secs |
| 17 | +---|--- |
| 18 | +cmp_func|0.0054 |
| 19 | +pandas|0.0061 |
| 20 | +reversor|0.0149 |
| 21 | +msorted|0.0179 |
| 22 | + |
| 23 | +As you can see, if the `cmp_func` is by far the fastest methodology as long as the number of cells in the table are 500 rows for 5 columns. However for larger data sets, `pandas` is the performance winner and scales extremely well. In such large dataset cases, where performance is key, `pandas` should be the first choice. |
| 24 | + |
| 25 | +The surprising thing from testing is that `cmp_func` far outperforms `reversor` which which is the only other methodology for multi-columnar sorting that can handle `NoneType` values. |
| 26 | + |
| 27 | +### Note on `NoneType` and sorting |
| 28 | +If your data may contain None, it would be wise to ensure your sort algorithm is tuned to handle them. This is because sorted uses `<` comparisons; which is not supported by `NoneType`. For example, the following error will result: `TypeError: '>' not supported between instances of 'NoneType' and 'str'`. |
| 29 | + |
| 30 | +### Methodologies |
| 31 | +Method|Descr|Notes |
| 32 | +---|---|--- |
| 33 | +cmp_func|Multi column sorting in the model `java.util.Comparator`|Fastest for small to medium size data |
| 34 | +reversor|Enable multi column sorting with column specific reverse sorting|Medium speed. [Source](https://stackoverflow.com/a/56842689/286807) |
| 35 | +msorted|Simple one-liner designed after `multisort` [example from python docs](https://docs.python.org/3/howto/sorting.html#sort-stability-and-complex-sorts)|Slowest of the bunch but not by much |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +### Dictionary Examples |
| 40 | +For data: |
| 41 | +``` |
| 42 | +rows_dict = [ |
| 43 | + {'idx': 0, 'name': 'joh', 'grade': 'C', 'attend': 100} |
| 44 | + ,{'idx': 1, 'name': 'jan', 'grade': 'a', 'attend': 80} |
| 45 | + ,{'idx': 2, 'name': 'dav', 'grade': 'B', 'attend': 85} |
| 46 | + ,{'idx': 3, 'name': 'bob' , 'grade': 'C', 'attend': 85} |
| 47 | + ,{'idx': 4, 'name': 'jim' , 'grade': 'F', 'attend': 55} |
| 48 | + ,{'idx': 5, 'name': 'joe' , 'grade': None, 'attend': 55} |
| 49 | +] |
| 50 | +``` |
| 51 | + |
| 52 | +### `msorted` |
| 53 | +Sort rows_dict by _grade_, descending, then _attend_, ascending and put None first in results: |
| 54 | +``` |
| 55 | +from multisort import msorted |
| 56 | +rows_sorted = msorted(rows_dict, [ |
| 57 | + ('grade', {'reverse': False, 'none_first': True}) |
| 58 | + ,'attend' |
| 59 | +]) |
| 60 | +
|
| 61 | +``` |
| 62 | + |
| 63 | +Sort rows_dict by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 64 | +``` |
| 65 | +from multisort import msorted |
| 66 | +rows_sorted = msorted(rows_dict, [ |
| 67 | + ('grade', {'reverse': False, 'clean': lambda s:None if s is None else s.upper()}) |
| 68 | + ,'attend' |
| 69 | +]) |
| 70 | +
|
| 71 | +``` |
| 72 | + |
| 73 | +### `sorted` with `reversor` |
| 74 | +Sort rows_dict by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 75 | +``` |
| 76 | +rows_sorted = sorted(rows_dict, key=lambda o: ( |
| 77 | + reversor(None if o['grade'] is None else o['grade'].upper()) |
| 78 | + ,o['attend']) |
| 79 | +)) |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +### `sorted` with `cmp_func` |
| 84 | +Sort rows_dict by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 85 | +``` |
| 86 | +def cmp_student(a,b): |
| 87 | + k='grade'; va=a[k]; vb=b[k] |
| 88 | + if va != vb: |
| 89 | + if va is None: return -1 |
| 90 | + if vb is None: return 1 |
| 91 | + return -1 if va > vb else 1 |
| 92 | + k='attend'; va=a[k]; vb=b[k]; |
| 93 | + if va != vb: return -1 if va < vb else 1 |
| 94 | + return 0 |
| 95 | +rows_sorted = sorted(rows_dict, key=cmp_func(cmp_student), reverse=True) |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +### Object Examples |
| 101 | +For data: |
| 102 | +``` |
| 103 | +class Student(): |
| 104 | + def __init__(self, idx, name, grade, attend): |
| 105 | + self.idx = idx |
| 106 | + self.name = name |
| 107 | + self.grade = grade |
| 108 | + self.attend = attend |
| 109 | + def __str__(self): return f"name: {self.name}, grade: {self.grade}, attend: {self.attend}" |
| 110 | + def __repr__(self): return self.__str__() |
| 111 | +
|
| 112 | +rows_obj = [ |
| 113 | + Student(0, 'joh', 'C', 100) |
| 114 | + ,Student(1, 'jan', 'a', 80) |
| 115 | + ,Student(2, 'dav', 'B', 85) |
| 116 | + ,Student(3, 'bob', 'C', 85) |
| 117 | + ,Student(4, 'jim', 'F', 55) |
| 118 | + ,Student(5, 'joe', None, 55) |
| 119 | +] |
| 120 | +``` |
| 121 | + |
| 122 | +### `msorted` |
| 123 | +(Same syntax as with 'dict' example) |
| 124 | + |
| 125 | + |
| 126 | +### `sorted` with `reversor` |
| 127 | +Sort rows_obj by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 128 | +``` |
| 129 | +rows_sorted = sorted(rows_obj, key=lambda o: ( |
| 130 | + reversor(None if o.grade is None else o.grade.upper()) |
| 131 | + ,o.attend) |
| 132 | +)) |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +### `sorted` with `cmp_func` |
| 137 | +Sort rows_obj by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 138 | +``` |
| 139 | +def cmp_student(a,b): |
| 140 | + if a.grade != b.grade: |
| 141 | + if a.grade is None: return -1 |
| 142 | + if b.grade is None: return 1 |
| 143 | + return -1 if a.grade > b.grade else 1 |
| 144 | + if a.attend != b.attend: |
| 145 | + return -1 if a.attend < b.attend else 1 |
| 146 | + return 0 |
| 147 | +rows_sorted = sorted(rows_obj, key=cmp_func(cmp_student), reverse=True) |
| 148 | +``` |
| 149 | + |
| 150 | + |
| 151 | +### List / Tuple Examples |
| 152 | +For data: |
| 153 | +``` |
| 154 | +rows_tuple = [ |
| 155 | + (0, 'joh', 'a' , 100) |
| 156 | + ,(1, 'joe', 'B' , 80) |
| 157 | + ,(2, 'dav', 'A' , 85) |
| 158 | + ,(3, 'bob', 'C' , 85) |
| 159 | + ,(4, 'jim', None , 55) |
| 160 | + ,(5, 'jan', 'B' , 70) |
| 161 | +] |
| 162 | +(COL_IDX, COL_NAME, COL_GRADE, COL_ATTEND) = range(0,4) |
| 163 | +``` |
| 164 | + |
| 165 | +### `msorted` |
| 166 | +Sort rows_tuple by _grade_, descending, then _attend_, ascending and put None first in results: |
| 167 | +``` |
| 168 | +from multisort import msorted |
| 169 | +rows_sorted = msorted(rows_tuple, [ |
| 170 | + (COL_GRADE, {'reverse': False, 'none_first': True}) |
| 171 | + ,COL_ATTEND |
| 172 | +]) |
| 173 | +
|
| 174 | +``` |
| 175 | + |
| 176 | + |
| 177 | +### `sorted` with `reversor` |
| 178 | +Sort rows_tuple by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 179 | +``` |
| 180 | +rows_sorted = sorted(rows_tuple, key=lambda o: ( |
| 181 | + reversor(None if o[COL_GRADE] is None else o[COL_GRADE].upper()) |
| 182 | + ,o[COL_ATTEND]) |
| 183 | +)) |
| 184 | +``` |
| 185 | + |
| 186 | + |
| 187 | +### `sorted` with `cmp_func` |
| 188 | +Sort rows_tuple by _grade_, descending, then _attend_ and call upper() for _grade_: |
| 189 | +``` |
| 190 | +def cmp_student(a,b): |
| 191 | + k=COL_GRADE; va=a[k]; vb=b[k] |
| 192 | + if va != vb: |
| 193 | + if va is None: return -1 |
| 194 | + if vb is None: return 1 |
| 195 | + return -1 if va > vb else 1 |
| 196 | + k=COL_ATTEND; va=a[k]; vb=b[k]; |
| 197 | + if va != vb: |
| 198 | + return -1 if va < vb else 1 |
| 199 | + return 0 |
| 200 | +rows_sorted = sorted(rows_tuple, key=cmp_func(cmp_student), reverse=True) |
| 201 | +``` |
| 202 | + |
| 203 | +### Tests / Samples |
| 204 | +Name|Descr|Other |
| 205 | +---|---|--- |
| 206 | +tests/test_msorted.py|msorted unit tests|- |
| 207 | +tests/performance_tests.py|Tunable performance tests using asyncio | requires pandas |
| 208 | +tests/hand_test.py|Hand testing|- |
0 commit comments