From ec5938e508526a70962e770ddd811f020951d1e8 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Wed, 15 Nov 2023 14:42:02 +0100 Subject: clock: Simplify sort_locations_by_time() There is no need to return -1 or 1 specifically, anything on the right side of 0 is OK, so simplify using the usual `a - b` implementation for such sort functions. Note that the type is OK here, as the `struct tm` are ints, the same as the function's return value. --- applets/clock/clock.c | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/applets/clock/clock.c b/applets/clock/clock.c index 840a45f7..309d4884 100644 --- a/applets/clock/clock.c +++ b/applets/clock/clock.c @@ -1187,35 +1187,17 @@ sort_locations_by_time (gconstpointer a, gconstpointer b) clock_location_localtime (loc_a, &tm_a); clock_location_localtime (loc_b, &tm_b); - ret = (tm_a.tm_year == tm_b.tm_year) ? 0 : 1; - if (ret) { - return (tm_a.tm_year < tm_b.tm_year) ? -1 : 1; - } - - ret = (tm_a.tm_mon == tm_b.tm_mon) ? 0 : 1; - if (ret) { - return (tm_a.tm_mon < tm_b.tm_mon) ? -1 : 1; - } - - ret = (tm_a.tm_mday == tm_b.tm_mday) ? 0 : 1; - if (ret) { - return (tm_a.tm_mday < tm_b.tm_mday) ? -1 : 1; - } - - ret = (tm_a.tm_hour == tm_b.tm_hour) ? 0 : 1; - if (ret) { - return (tm_a.tm_hour < tm_b.tm_hour) ? -1 : 1; - } - - ret = (tm_a.tm_min == tm_b.tm_min) ? 0 : 1; - if (ret) { - return (tm_a.tm_min < tm_b.tm_min) ? -1 : 1; - } - - ret = (tm_a.tm_sec == tm_b.tm_sec) ? 0 : 1; - if (ret) { - return (tm_a.tm_sec < tm_b.tm_sec) ? -1 : 1; - } + ret = tm_a.tm_year - tm_b.tm_year; + if (! ret) + ret = tm_a.tm_mon - tm_b.tm_mon; + if (! ret) + ret = tm_a.tm_mday - tm_b.tm_mday; + if (! ret) + ret = tm_a.tm_hour - tm_b.tm_hour; + if (! ret) + ret = tm_a.tm_min - tm_b.tm_min; + if (! ret) + ret = tm_a.tm_sec - tm_b.tm_sec; return ret; } -- cgit v1.2.1