From f2a9d5541dbac0d63b91ffa36acdca5f11726c01 Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Thu, 16 May 2024 12:22:34 +0200 Subject: [PATCH] Alphabetically sort the channels by name --- SlackLogViewer/SlackLogViewer.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/SlackLogViewer/SlackLogViewer.cpp b/SlackLogViewer/SlackLogViewer.cpp index f9dcebc..c382da6 100644 --- a/SlackLogViewer/SlackLogViewer.cpp +++ b/SlackLogViewer/SlackLogViewer.cpp @@ -365,12 +365,28 @@ void SlackLogViewer::LoadChannels() const QJsonArray& arr = channels.array(); if (arr.size() == 0) return; + QVector channelList; + for (const QJsonValue& val : arr) + { + channelList.append(val.toObject()); + } + + std::sort(channelList.begin(), channelList.end(), [](const QJsonObject& a, const QJsonObject& b) { + return a["name"].toString().compare(b["name"].toString(), Qt::CaseInsensitive) < 0; + }); + + QJsonArray sortedArr; + for (const QJsonObject& obj : channelList) + { + sortedArr.append(obj); + } + ChannelTreeModel* model = static_cast(mChannelView->model()); QModelIndex index = model->index((int)Channel::CHANNEL, 0, QModelIndex()); - model->insertRows(0, arr.size(), index); + model->insertRows(0, sortedArr.size(), index); int row = 0; - for (auto c : arr) + for (auto c : sortedArr) { const QString& id = c.toObject()["id"].toString(); const QString& name = c.toObject().value("name").toString(); @@ -385,7 +401,7 @@ void SlackLogViewer::LoadChannels() ++row; } - for (int i = 0; i < arr.size(); ++i) + for (int i = 0; i < sortedArr.size(); ++i) { MessageListView* mw = new MessageListView(); mw->setModel(new MessageListModel(mw));