From c00e0a7ecb5fd5af0f2edb69678538b860678d1f Mon Sep 17 00:00:00 2001 From: "Christian T. Jacobs" Date: Wed, 28 Jun 2017 16:22:53 +0100 Subject: [PATCH] Fixed direction of sorting. --- CHANGELOG.md | 1 + pyqso/compare.py | 27 +++++++++++++++++---------- tests/test_compare.py | 10 +++++----- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b592cf..66582b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ ### Fixed - Any characters in the DX cluster server's reponse that cannot be decoded are now replaced with a replacement marker in the DX cluster frame. - Fixed the QSO index used in the Gtk.ListStore. Just before a QSO is added with add_record it was assumed that it's index would be max(rowid)+1, which is not always the case. This led to inconsistencies between the Gtk.ListStore and the database. Indices used in the Gtk.ListStore are now obtained directly from the database after insertion. +- Direction of sorting. ## [0.3] - 2016-05-28 ### Added diff --git a/pyqso/compare.py b/pyqso/compare.py index 245c2dc..8f532e1 100644 --- a/pyqso/compare.py +++ b/pyqso/compare.py @@ -25,7 +25,7 @@ def compare_date_and_time(model, row1, row2, user_data): :arg Gtk.TreeIter row1: The pointer to row A. :arg Gtk.TreeIter row2: The pointer to row B. :arg user_data: The specific column from which to retrieve data for rows A and B. - :returns: 1 if Row B's date/time is more recent than Row A's; 0 if both dates and times are the same; -1 if Row A's date/time is more recent than Row B's. + :returns: -1 if Row B's date/time is more recent than Row A's; 0 if both dates and times are the same; 1 if Row A's date/time is more recent than Row B's. :rtype: int """ date1 = model.get_value(row1, user_data[0]) @@ -33,17 +33,17 @@ def compare_date_and_time(model, row1, row2, user_data): time1 = model.get_value(row1, user_data[1]) time2 = model.get_value(row2, user_data[1]) if(date1 < date2): - return 1 + return -1 elif(date1 == date2): # If the dates are the same, then let's also sort by time. if(time1 > time2): - return -1 + return 1 elif(time1 == time2): return 0 else: - return 1 + return -1 else: - return -1 + return 1 def compare_default(model, row1, row2, user_data): @@ -53,14 +53,21 @@ def compare_default(model, row1, row2, user_data): :arg Gtk.TreeIter row1: The pointer to row A. :arg Gtk.TreeIter row2: The pointer to row B. :arg user_data: The specific column from which to retrieve data for rows A and B. - :returns: 1 if the value of Row A's column value is less than Row B's column value; 0 if both values are the same; -1 if Row A's column value is greater than Row B's column value. + :returns: -1 if the value of Row A's column value is less than Row B's column value; 0 if both values are the same; 1 if Row A's column value is greater than Row B's column value. :rtype: int """ - value1 = model.get_value(row1, user_data) - value2 = model.get_value(row2, user_data) + + # Let's try to deal with numerical values, if possible. + try: + value1 = float(model.get_value(row1, user_data)) + value2 = float(model.get_value(row2, user_data)) + except ValueError: + value1 = model.get_value(row1, user_data) + value2 = model.get_value(row2, user_data) + if(value1 < value2): - return 1 + return -1 elif(value1 == value2): return 0 else: - return -1 + return 1 diff --git a/tests/test_compare.py b/tests/test_compare.py index b1aeda6..76acfde 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -54,11 +54,11 @@ class TestCompare(unittest.TestCase): # Compare values in the second column. column_index = 1 result = compare_default(self.model, iter1, iter2, column_index) - assert(result == 1) + assert(result == -1) result = compare_default(self.model, iter2, iter3, column_index) assert(result == 0) result = compare_default(self.model, iter3, iter4, column_index) - assert(result == -1) + assert(result == 1) def test_compare_date_and_time(self): """ Check that dates in yyyymmdd format are compared correctly. """ @@ -73,13 +73,13 @@ class TestCompare(unittest.TestCase): # Compare values in the third (and fourth, if necessary) column. column_index = 2 result = compare_date_and_time(self.model, iter1, iter2, [column_index, column_index+1]) - assert(result == 1) + assert(result == -1) result = compare_date_and_time(self.model, iter2, iter3, [column_index, column_index+1]) - assert(result == 1) + assert(result == -1) result = compare_date_and_time(self.model, iter3, iter4, [column_index, column_index+1]) assert(result == 0) result = compare_date_and_time(self.model, iter4, iter1, [column_index, column_index+1]) - assert(result == -1) + assert(result == 1) if(__name__ == '__main__'): unittest.main()