Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sorting data based on key
QtWS25 Last Chance

Sorting data based on key

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 713 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    npatil15
    wrote on last edited by
    #1

    Hi,

    I have data in a format like as,

    QList<QPair<QString, QJsonValue>> sessionList
    

    Check the sample data of sessionList

    QPair("2717b2bb-aa1f-460e-b6ff-c932114a8b99",QJsonValue(object, QJsonObject({"CreatedAt":"2019-06-18T10:34:37.3501135","FileName":"imgLeft8bitColor_000000050.png","FrameIndex":30,"ImageGuid":"76d453ed-0950-424d-952f-b20d1f0c921f","IsKeyframe":false, "TabGuid":"2717b2bb-aa1f-460e-b6ff-c932114a8b99"})))
    QPair("078f7119-844d-4c31-9eee-c695f2c9adbb",QJsonValue(object, QJsonObject({"CreatedAt":"2019-06-18T11:21:58.5620774","FileName":"imgLeft8bitColor_000000050.png","FrameIndex":30,"ImageGuid":"3c9517ae-6c96-4746-8279-1c143339f576","IsKeyframe":false, "TabGuid":"078f7119-844d-4c31-9eee-c695f2c9adbb"})))
    

    Like above I have more data that I want to sort based on QPair.first(consider as key)(say, "078f7119-844d-4c31-9eee-c695f2c9adbb").
    So what algorithm I should use which sort my QJsonValue for the same type of key in one container?

    Note: Key count can vary

    JKSHJ 1 Reply Last reply
    0
    • N Offline
      N Offline
      npatil15
      wrote on last edited by npatil15
      #2

      I found one solution for that, use QMultiHash or QMultiMap, here it can allow adding multiple data under the same key, so in that way, I can distinguish or sort data.

      But still, I'm looking for an option where I can have one key and data can be added under that key only, not the way QMultiHash work, as it stores key and data in a separate list even if keys are same.

      1 Reply Last reply
      0
      • N npatil15

        Hi,

        I have data in a format like as,

        QList<QPair<QString, QJsonValue>> sessionList
        

        Check the sample data of sessionList

        QPair("2717b2bb-aa1f-460e-b6ff-c932114a8b99",QJsonValue(object, QJsonObject({"CreatedAt":"2019-06-18T10:34:37.3501135","FileName":"imgLeft8bitColor_000000050.png","FrameIndex":30,"ImageGuid":"76d453ed-0950-424d-952f-b20d1f0c921f","IsKeyframe":false, "TabGuid":"2717b2bb-aa1f-460e-b6ff-c932114a8b99"})))
        QPair("078f7119-844d-4c31-9eee-c695f2c9adbb",QJsonValue(object, QJsonObject({"CreatedAt":"2019-06-18T11:21:58.5620774","FileName":"imgLeft8bitColor_000000050.png","FrameIndex":30,"ImageGuid":"3c9517ae-6c96-4746-8279-1c143339f576","IsKeyframe":false, "TabGuid":"078f7119-844d-4c31-9eee-c695f2c9adbb"})))
        

        Like above I have more data that I want to sort based on QPair.first(consider as key)(say, "078f7119-844d-4c31-9eee-c695f2c9adbb").
        So what algorithm I should use which sort my QJsonValue for the same type of key in one container?

        Note: Key count can vary

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @npatil15 said in Sorting data based on key:

        So what algorithm I should use which sort my QJsonValue for the same type of key in one container?

        Which do you do more often: Look up your data by key, or look up your data by index? If you look-up by key, use a map instead of a list as your data container. A map also automatically sorts all your data by key.

        // Writing data
        QMap<QString, QJsonValue> sessionMap;
        sessionMap["2717b2bb-aa1f-460e-b6ff-c932114a8b99"] = QJsonObject(...);
        sessionMap["078f7119-844d-4c31-9eee-c695f2c9adbb"] = QJsonObject(...);
        
        ...
        
        // Reading data
        QJsonValue val = sessionMap["2717b2bb-aa1f-460e-b6ff-c932114a8b99"];
        qDebug() << val;
        // Prints QJsonValue(object, QJsonObject({"CreatedAt":"2019-06-18T11:21:58.5620774","FileName":"imgLeft8bitColor_000000050.png","FrameIndex":30,"ImageGuid":"3c9517ae-6c96-4746-8279-1c143339f576","IsKeyframe":false, "TabGuid":"078f7119-844d-4c31-9eee-c695f2c9adbb"})))
        
        

        If you don't want to switch to a map, you could use std::sort() and give it a comparison function:

        std::sort(
                sessionList.begin(),
                sessionList.end(),
                [](const QPair<QString, QJsonValue> &a, const QPair<QString, QJsonValue> &b)->bool
        {
           return a.first < b.first;
        });
        

        Finally, even if you don't want to change to a map, you should use a QVector, not a QList: https://marcmutz.wordpress.com/effective-qt/containers/

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4
        • N Offline
          N Offline
          npatil15
          wrote on last edited by
          #4

          Thanks for the reply,

          But tell me what if I have the same key for multiple data, then QMap will update the data and erase the previous one, isn't it? That I don't want.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            https://doc.qt.io/qt-5/qmultimap.html

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3
            • N Offline
              N Offline
              npatil15
              wrote on last edited by
              #6

              Found solution, using below I can achieve to store multiple QJsonValues under a single key, and it works well.

              QMap<QString, QList<QJsonValue>> map;
              const QJsonValue v;
              
              map["Unique ID in String"] << jsonvalues;
              

              Hope it help others :)

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                You don't need it.
                QMap<QString, QJsonValue> map; and then map.insertMulti(QStringLiteral("Unique ID in String"),QJsonValue(/*...*/));

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                5

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved