From 9bb58338485c5c938b529a3ec643a985031f0c97 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 15 May 2019 11:40:39 +0200 Subject: Undo: sort list of dives to add and delete In 5729f93e1f512aa9e68b0e01743152aaee2b7c12, the dive addition / deletion code was simplified in that indexes were calculated on the fly. This made it, in principle, possible to pass in dives in any order. But there was a small oversight: the recipients of the dives-added and dives-deleted signals expect the dives to be sorted as in the core list. Only then will the lists be consistent. Therefore, sort the lists before adding / deleting dives. Signed-off-by: Berthold Stoeger --- desktop-widgets/command_divelist.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp index c9303c5df..995a0ec15 100644 --- a/desktop-widgets/command_divelist.cpp +++ b/desktop-widgets/command_divelist.cpp @@ -100,6 +100,11 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite divesToAdd.reserve(divesAndSitesToDelete.dives.size()); sitesToAdd.reserve(divesAndSitesToDelete.sites.size()); + // Make sure that the dive list is sorted. The added dives will be sent in a signal + // and the recipients assume that the dives are sorted the same way as they are + // in the core list. + std::sort(divesAndSitesToDelete.dives.begin(), divesAndSitesToDelete.dives.end(), dive_less_than); + for (dive *d: divesAndSitesToDelete.dives) divesToAdd.push_back(removeDive(d, tripsToAdd)); divesAndSitesToDelete.dives.clear(); @@ -140,6 +145,13 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd) res.resize(toAdd.dives.size()); sites.reserve(toAdd.sites.size()); + // Make sure that the dive list is sorted. The added dives will be sent in a signal + // and the recipients assume that the dives are sorted the same way as they are + // in the core list. + std::sort(toAdd.dives.begin(), toAdd.dives.end(), + [](const DiveToAdd &d, const DiveToAdd &d2) + { return dive_less_than(d.dive.get(), d2.dive.get()); }); + // Now, add the dives // Note: the idiomatic STL-way would be std::transform, but let's use a loop since // that is closer to classical C-style. -- cgit v1.2.3-70-g09d2