Skip to content
Snippets Groups Projects
Commit a61db892 authored by Studer Gabriel's avatar Studer Gabriel
Browse files

Custom comparison function in sorting algorithms must be provided as "key".

A simple function returing True or False is also not sufficient anymore.
functools.cmp_to_key comes to the rescue.
parent 2841fd31
No related branches found
No related tags found
No related merge requests found
......@@ -1043,8 +1043,20 @@ Statistics for column %(col)s
sign=1
key_index=self.GetColIndex(by)
def _key_cmp(lhs, rhs):
return sign*cmp(lhs[key_index], rhs[key_index])
self.rows=sorted(self.rows, _key_cmp)
a = lhs[key_index]
b = rhs[key_index]
# mimic behaviour of the cmp function from Python2 that happily
# compared None values
if a is None or b is None:
if a is None and b is not None:
return -1 * sign
if b is None and a is not None:
return 1 * sign
return 0
return sign*((a > b) - (a < b))
import functools
self.rows=sorted(self.rows, key=functools.cmp_to_key(_key_cmp))
def GetUnique(self, col, ignore_nan=True):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment