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. QT std::sort function is not sorting correctly
Forum Updated to NodeBB v4.3 + New Features

QT std::sort function is not sorting correctly

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 4.2k 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.
  • A Offline
    A Offline
    Ayush Gupta
    wrote on 17 Oct 2019, 10:24 last edited by
    #1

    I have below structure.

    struct test
    {
    uint id;
    QString name;
    };

    I have below map which I am inserting element.
    map<uint, test> maptest1;
    maptest1.insert ( 1, {1,"Ram"));
    maptest2.insert( 2, { 1,"Shyam"));
    maptest3.insert(3, {2,"Mohan"));

    Now I am trying to sort this map based on ID of test struct.

    I did below copied all things in vector

    vector<test> test1;
    for(const test& a : maptest.values())
    {
    test1.push_back(a);
    }

    Now I am sorting the vector using below function
    std::sort(test1.begin() , test2.begin(), [this]( const test& test1 , const test& test2 ) { return test1.id < test2.id});

    But sorting does not works fine for those who have same id in test (like ram and shyam).

    I am working with some more large numbers of such instances this is just as an example.

    Can anyone help me

    J 1 Reply Last reply 17 Oct 2019, 10:37
    0
    • A Ayush Gupta
      17 Oct 2019, 10:24

      I have below structure.

      struct test
      {
      uint id;
      QString name;
      };

      I have below map which I am inserting element.
      map<uint, test> maptest1;
      maptest1.insert ( 1, {1,"Ram"));
      maptest2.insert( 2, { 1,"Shyam"));
      maptest3.insert(3, {2,"Mohan"));

      Now I am trying to sort this map based on ID of test struct.

      I did below copied all things in vector

      vector<test> test1;
      for(const test& a : maptest.values())
      {
      test1.push_back(a);
      }

      Now I am sorting the vector using below function
      std::sort(test1.begin() , test2.begin(), [this]( const test& test1 , const test& test2 ) { return test1.id < test2.id});

      But sorting does not works fine for those who have same id in test (like ram and shyam).

      I am working with some more large numbers of such instances this is just as an example.

      Can anyone help me

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 17 Oct 2019, 10:37 last edited by
      #2

      hi, @Ayush-Gupta

      why don't you modify your test lambda then?

      std::sort(test1.begin() , test2.begin(), [this]( const test& test1 , const test& test2 )->bool { 
          if(test1.id == test2.id) {
              //case same ID -> your other compare method
              return other;
          }
          return test1.id < test2.id
      });
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Ayush Gupta
        wrote on 17 Oct 2019, 10:40 last edited by
        #3

        @J-Hilk I did like this but not works
        std::sort(test1.begin() , test2.begin(), [this]( const test& test1 , const test& test2 )->bool {
        if(test1.id == test2.id) {
        return false;
        }
        return test1.id < test2.id
        });

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 17 Oct 2019, 10:48 last edited by VRonin
          #4
          • what you want is probably just to use std::stable_sort instead.
          • test1.begin() , test2.begin() What is this? Normally it's test1.begin() , test1.end()
          • for(const test& a : maptest.values()) test1.push_back(a); As far as I remember std::map diesn't have a values() method. whatever you are using, the values method propably already returns a sortable unidimensional container of the values, there is no need to copy them over to a 3rd container

          "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
          4
          • A Offline
            A Offline
            Ayush Gupta
            wrote on 17 Oct 2019, 10:50 last edited by Ayush Gupta
            #5

            @VRonin That is typo I am using
            test1.begin() , test2.end().
            Why you think std::stable_sort will work instead of sort?

            A V 2 Replies Last reply 17 Oct 2019, 10:51
            0
            • A Ayush Gupta
              17 Oct 2019, 10:50

              @VRonin That is typo I am using
              test1.begin() , test2.end().
              Why you think std::stable_sort will work instead of sort?

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 17 Oct 2019, 10:51 last edited by
              #6

              @Ayush-Gupta said in QT std::sort function is not sorting correctly:

              test1.begin() , test2.end()

              Which is wrong too. Both most be from the same data structure.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              3
              • A Ayush Gupta
                17 Oct 2019, 10:50

                @VRonin That is typo I am using
                test1.begin() , test2.end().
                Why you think std::stable_sort will work instead of sort?

                V Offline
                V Offline
                VRonin
                wrote on 17 Oct 2019, 10:53 last edited by VRonin
                #7

                @Ayush-Gupta said in QT std::sort function is not sorting correctly:

                Why you think std::stable_sort will work instead of sort?

                The only difference between sort and stable_sort is how they handle items with the same value. stable_sort keeps them in the same relative order as the original vector

                "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
                2
                • J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 17 Oct 2019, 10:54 last edited by VRonin
                  #8

                  actually, didn't see the error @VRonin mentioned, with that fixed, it works

                  for example:

                  QVector<test> maptest1;
                      maptest1.append( {1,"Ram"});
                      maptest1.append( { 1,"Shyam"});
                      maptest1.append( {2,"Mohan"});
                  
                      std::sort(maptest1.begin() , maptest1.end(), [=]( const test& test1 , const test& test2 )->bool {
                          if(test1.id == test2.id) {
                              //case same ID -> your other compare method
                              return test1.name.at(0) > test2.name.at(0);
                          }
                          return test1.id < test2.id;
                      });
                      for(test a: maptest1){
                          qDebug() << a.id << a.name;
                      }
                  //output:
                  // 1 "Shyam"
                  //1 "Ram"
                  //2 "Mohan"
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  3
                  • A Offline
                    A Offline
                    Ayush Gupta
                    wrote on 17 Oct 2019, 11:01 last edited by
                    #9

                    @VRonin You are right stable_sort works perfect for me.
                    All many thanks for your help.

                    A 1 Reply Last reply 17 Oct 2019, 11:08
                    2
                    • A Ayush Gupta
                      17 Oct 2019, 11:01

                      @VRonin You are right stable_sort works perfect for me.
                      All many thanks for your help.

                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 17 Oct 2019, 11:08 last edited by
                      #10

                      @Ayush-Gupta So please mark this topic as SOLVED now. Thanks!

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      2

                      4/10

                      17 Oct 2019, 10:48

                      • Login

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