diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-05-15 11:48:41 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-05-17 07:55:34 -0700 |
commit | 77b5d714fb1c373d0f2c93c23607958ea9788b4b (patch) | |
tree | 844425a40d0771daa785cd99154b9208652e7932 /core/divelist.c | |
parent | 9bb58338485c5c938b529a3ec643a985031f0c97 (diff) | |
download | subsurface-77b5d714fb1c373d0f2c93c23607958ea9788b4b.tar.gz |
Core: consider dive computers when sorting dives
When splitting out dive computers, the dives were sorted in
an arbitrary way (according to an internal id), since all
data are identical.
Therefore, consider the dive-computer model names when sorting
dives. Equal dives are now sorted alphabetically by model.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divelist.c')
-rw-r--r-- | core/divelist.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/core/divelist.c b/core/divelist.c index bd29b9fda..b342e1cd3 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -769,6 +769,29 @@ struct dive *last_selected_dive() return ret; } +/* Like strcmp(), but don't crash on null-pointers */ +static int safe_strcmp(const char *s1, const char *s2) +{ + return strcmp(s1 ? s1 : "", s2 ? s2 : ""); +} + +/* Compare a list of dive computers by model name */ +static int comp_dc(const struct divecomputer *dc1, const struct divecomputer *dc2) +{ + int cmp; + while (dc1 || dc2) { + if (!dc1) + return -1; + if (!dc2) + return 1; + if ((cmp = safe_strcmp(dc1->model, dc2->model)) != 0) + return cmp; + dc1 = dc1->next; + dc2 = dc2->next; + } + return 0; +} + /* This function defines the sort ordering of dives. The core * and the UI models should use the same sort function, which * should be stable. This is not crucial at the moment, as the @@ -788,6 +811,7 @@ struct dive *last_selected_dive() */ static int comp_dives(const struct dive *a, const struct dive *b) { + int cmp; if (a->when < b->when) return -1; if (a->when > b->when) @@ -806,6 +830,8 @@ static int comp_dives(const struct dive *a, const struct dive *b) return -1; if (a->number > b->number) return 1; + if ((cmp = comp_dc(&a->dc, &b->dc)) != 0) + return cmp; if (a->id < b->id) return -1; if (a->id > b->id) |