From a61db892d09332495fc673c4340bcc502fd9ddb1 Mon Sep 17 00:00:00 2001 From: Gabriel Studer <gabriel.studer@unibas.ch> Date: Fri, 15 Nov 2019 10:00:41 +0100 Subject: [PATCH] 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. --- modules/base/pymod/table.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 6a8a9abe3..dc59194e0 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -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): """ -- GitLab