diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py
index 6a8a9abe35be3efe25abe1560eea7c820609c4f9..dc59194e08c1ef75ee1505e0e59d209a85c5495d 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):
     """