Frank, I am not commenting on your specific code, just asking a question.
If you have two tuples of the same size it does seem that python evaluates them in the order you want just by doing something like this example I made with a 2-tuple: >>> (1, 2) > (2,1) False >>> (1, 2) > (1,1) True >>> (1, 1) > (1,1) False So if you take your database entries and re-order them so the keys you want to sort by are in order from left to right, and you don't include any that you don't want to sort by, the above technique may work well enough. Of course, you may want to re-establish the order and contents afterward. Of course if you have some None, that messes things up: >>> (1, 1) > (1,None) Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> (1, 1) > (1,None) TypeError: '>' not supported between instances of 'int' and 'NoneType' Unless, of course, they are both None. >>> (1, None) > (1,None) False For python 2.x there was a cmp() function that might have met your needs. It was removed in 3.x https://docs.python.org/3.0/whatsnew/3.0.html " The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)" But as noted this does not work with your None. Final comment meant as humor. The following will not work even though it is complex! >>> (1 + 2j, 1 - 2j) > (1 + 3j,2 +4j) Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> (1 + 2j, 1 - 2j) > (1 + 3j,2 +4j) TypeError: '>' not supported between instances of 'complex' and 'complex' And the reason is obvious as complex numbers have two dimensions so you might need to compare something like their magnitude as in the length of a vector ... -----Original Message----- From: Python-list <python-list-bounces+avigross=verizon....@python.org> On Behalf Of Frank Millman Sent: Tuesday, December 18, 2018 5:50 AM To: python-list@python.org Subject: Complex 'compare' Hi all I want to compare two tuples. They each represent a row in a database, and each element represents a column, so I will use that terminology. I need to know if one row is greater than or less than the other. The sort sequence can be complex - one or more columns, each of which can be sorted ascending or descending. Below is the function I have come up with. Can anyone see any problem with it, or suggest a better way to do it? I have taken one short cut. If all sort columns compare equal, it will return True, which is wrong. In practice I ensure that one of the sort columns will be the primary key, so the two rows should never be equal. Thanks Frank Millman def compare(source_row, target_row, order, compare_type): # source_row - the row I want to compare - some sort columns could contain None # target_row - the row I want to compare it with - no sort columns will contain None # order - a list of 2-part tuples defining the sort sequence - the column number, and True if descending else False # compare_type - either 'gt' or 'lt' def compare_col(source_col, target_col, desc, compare_type): if compare_type == 'gt': if desc: return True if source_col is None else source_col < target_col else: return False if source_col is None else source_col > target_col elif compare_type == 'lt': if desc: return False if source_col is None else source_col > target_col else: return True if source_col is None else source_col < target_col for pos, desc in order: source_col = source_row[pos] target_col = target_row[pos] if compare_col(source_col, target_col, desc, compare_type): return True if source_col != target_col: return False # if we get here they are equal - # compare the next column in the sort sequence return True -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list