diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index d48c2c931..b36c9f725 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -11779,10 +11779,15 @@ std::pair> MessagesManager::search_dialogs(const string } vector MessagesManager::sort_dialogs_by_order(const vector &dialog_ids, int32 limit) const { - auto dialog_dates = transform(dialog_ids, [this](auto dialog_id) { - const Dialog *d = this->get_dialog(dialog_id); + int64 fake_order = static_cast(dialog_ids.size()) + 1; + auto dialog_dates = transform(dialog_ids, [this, &fake_order](DialogId dialog_id) { + const Dialog *d = get_dialog(dialog_id); CHECK(d != nullptr); - return DialogDate(d->order, dialog_id); + if (is_dialog_inited(d) || d->order != DEFAULT_ORDER) { + return DialogDate(d->order, dialog_id); + } + // if the dialog is not inited yet, we need to assume that server knows better and the dialog needs to be returned + return DialogDate(fake_order--, dialog_id); }); if (static_cast(limit) >= dialog_dates.size()) { std::sort(dialog_dates.begin(), dialog_dates.end());