Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. sort a vector or pair alphabetically.
Qt 6.11 is out! See what's new in the release blog

sort a vector or pair alphabetically.

Scheduled Pinned Locked Moved Solved C++ Gurus
10 Posts 4 Posters 4.8k Views 1 Watching
  • 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
    Natural_Bugger
    wrote on last edited by
    #1
    #include <iostream>     // std::cout
    #include <algorithm>    // std::sort
    #include <vector>       // std::vector
    
    bool compareFunction (std::string a, std::string b) {return a<b;} 
    //compare any way you like, here I am using the default string comparison
    
    int main () 
    {
      std::string myNames[] = {"Henry","Tom","Jafar","Alice","Bob","Cindy","Clara","Michael"};
    
      std::vector<std::string> myvector (myNames, myNames+8); //create vector from array
    
      std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
    
    
      std::cout << "Sorted vector:";
      for (std::vector<std::string>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
            std::cout << ' ' << *it;
      std::cout << '\n';
    
      return 0;
    }
    

    http://cpp.sh/7hrvf

    how would this apply to my vector?

    vector< pair <string, string> > data; // name / email
    

    takes 2 arguments.

    bool compareFunction (std::string a, std::string b) {return a<b;} 
    

    but i don't see these arguments applied here.

    std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
    

    i need the "key" value of my pairs fully alphabetically sorted up to the last char of the name.

    regards.

    Pablo J. RoginaP JonBJ 2 Replies Last reply
    0
    • N Natural_Bugger
      #include <iostream>     // std::cout
      #include <algorithm>    // std::sort
      #include <vector>       // std::vector
      
      bool compareFunction (std::string a, std::string b) {return a<b;} 
      //compare any way you like, here I am using the default string comparison
      
      int main () 
      {
        std::string myNames[] = {"Henry","Tom","Jafar","Alice","Bob","Cindy","Clara","Michael"};
      
        std::vector<std::string> myvector (myNames, myNames+8); //create vector from array
      
        std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
      
      
        std::cout << "Sorted vector:";
        for (std::vector<std::string>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
              std::cout << ' ' << *it;
        std::cout << '\n';
      
        return 0;
      }
      

      http://cpp.sh/7hrvf

      how would this apply to my vector?

      vector< pair <string, string> > data; // name / email
      

      takes 2 arguments.

      bool compareFunction (std::string a, std::string b) {return a<b;} 
      

      but i don't see these arguments applied here.

      std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
      

      i need the "key" value of my pairs fully alphabetically sorted up to the last char of the name.

      regards.

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @Natural_Bugger sorry for asking, but how is this issue related to Qt specifically?

      It seems like an initial assignment in a programming course

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      N 1 Reply Last reply
      1
      • Pablo J. RoginaP Pablo J. Rogina

        @Natural_Bugger sorry for asking, but how is this issue related to Qt specifically?

        It seems like an initial assignment in a programming course

        N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by
        #3

        @Pablo-J-Rogina

        well, I'm solving some online programming "riddles" and since i use Qt creator, because i like it.
        been rusty for years.

        J.HilkJ 1 Reply Last reply
        0
        • N Natural_Bugger

          @Pablo-J-Rogina

          well, I'm solving some online programming "riddles" and since i use Qt creator, because i like it.
          been rusty for years.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Natural_Bugger

          bool compareFunction (std::pair<std::string, std::string> a, std::pair<std::string, std::string> b) {return a.first<b.first;}

          btw, this should, even for std::string be const references

          bool compareFunction (const std::pair<std::string, std::string> &a, const std::pair<std::string, std::string> &b) {return a.first<b.first;}


          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.

          N 1 Reply Last reply
          1
          • N Natural_Bugger
            #include <iostream>     // std::cout
            #include <algorithm>    // std::sort
            #include <vector>       // std::vector
            
            bool compareFunction (std::string a, std::string b) {return a<b;} 
            //compare any way you like, here I am using the default string comparison
            
            int main () 
            {
              std::string myNames[] = {"Henry","Tom","Jafar","Alice","Bob","Cindy","Clara","Michael"};
            
              std::vector<std::string> myvector (myNames, myNames+8); //create vector from array
            
              std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
            
            
              std::cout << "Sorted vector:";
              for (std::vector<std::string>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
                    std::cout << ' ' << *it;
              std::cout << '\n';
            
              return 0;
            }
            

            http://cpp.sh/7hrvf

            how would this apply to my vector?

            vector< pair <string, string> > data; // name / email
            

            takes 2 arguments.

            bool compareFunction (std::string a, std::string b) {return a<b;} 
            

            but i don't see these arguments applied here.

            std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
            

            i need the "key" value of my pairs fully alphabetically sorted up to the last char of the name.

            regards.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Natural_Bugger
            If you wish to store std::pair() elements in your vector, you need a compareFunction() which takes std::pair() instead of std::string as its arguments (well, const references to them really, but that's not the point).

            1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @Natural_Bugger

              bool compareFunction (std::pair<std::string, std::string> a, std::pair<std::string, std::string> b) {return a.first<b.first;}

              btw, this should, even for std::string be const references

              bool compareFunction (const std::pair<std::string, std::string> &a, const std::pair<std::string, std::string> &b) {return a.first<b.first;}

              N Offline
              N Offline
              Natural_Bugger
              wrote on last edited by Natural_Bugger
              #6

              @J-Hilk said in sort a vector or pair alphabetically.:

              bool compareFunction (const std::pair<std::string, std::string> &a, const std::pair<std::string, std::string> &b) {return a.first<b.first;}

              indeed, thank you very much.
              i thought i had to expand the function and use some more "std::.." functions, but it worked perfectly.

              but I'm still a little clueless on how the function receives it arguments.

              @JonB

              also to you, thnx

              J.HilkJ JonBJ 2 Replies Last reply
              0
              • N Natural_Bugger

                @J-Hilk said in sort a vector or pair alphabetically.:

                bool compareFunction (const std::pair<std::string, std::string> &a, const std::pair<std::string, std::string> &b) {return a.first<b.first;}

                indeed, thank you very much.
                i thought i had to expand the function and use some more "std::.." functions, but it worked perfectly.

                but I'm still a little clueless on how the function receives it arguments.

                @JonB

                also to you, thnx

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @Natural_Bugger said in sort a vector or pair alphabetically.:

                but I'm still a little clueless on how the function receives it arguments.

                this is taken from the documentation

                b913145f-2796-44d9-956d-1e8d90125434-image.png

                The argument for the compare, expects a function that returns a bool and accepts 2 arguments


                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
                2
                • N Natural_Bugger

                  @J-Hilk said in sort a vector or pair alphabetically.:

                  bool compareFunction (const std::pair<std::string, std::string> &a, const std::pair<std::string, std::string> &b) {return a.first<b.first;}

                  indeed, thank you very much.
                  i thought i had to expand the function and use some more "std::.." functions, but it worked perfectly.

                  but I'm still a little clueless on how the function receives it arguments.

                  @JonB

                  also to you, thnx

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Natural_Bugger said in sort a vector or pair alphabetically.:

                  but I'm still a little clueless on how the function receives it arguments.

                  When you call std::sort() on a vector of objects, internally it will repeatedly call the function you pass as compareFunction on pairs of elements in the vector as it goes along, using its return result to re-order these elements in the vector, till they are fully sorted. All you need to know is that the argument types to compareFunction must match exactly the type of the elements in the vector, else you will get a compile-time type error.

                  N 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Natural_Bugger said in sort a vector or pair alphabetically.:

                    but I'm still a little clueless on how the function receives it arguments.

                    When you call std::sort() on a vector of objects, internally it will repeatedly call the function you pass as compareFunction on pairs of elements in the vector as it goes along, using its return result to re-order these elements in the vector, till they are fully sorted. All you need to know is that the argument types to compareFunction must match exactly the type of the elements in the vector, else you will get a compile-time type error.

                    N Offline
                    N Offline
                    Natural_Bugger
                    wrote on last edited by Natural_Bugger
                    #9

                    @JonB

                    thnx, i placed the sort function at the right spot in the first moment and it worked out perfectly first time.

                    it just wasn't obvious on how the "compareFunction" receives it's arguments.
                    that's clever, it wouldn't be obvious to most at first sight.

                    std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
                    

                    no arguments are passed to "compareFunction", whilst requiring 2 arguments

                    bool compareFunction (std::string a, std::string b) {return a<b;} 
                    

                    thnx @J-Hilk and @JonB

                    JonBJ 1 Reply Last reply
                    0
                    • N Natural_Bugger

                      @JonB

                      thnx, i placed the sort function at the right spot in the first moment and it worked out perfectly first time.

                      it just wasn't obvious on how the "compareFunction" receives it's arguments.
                      that's clever, it wouldn't be obvious to most at first sight.

                      std::sort(myvector.begin(),myvector.end(),compareFunction);//sort the vector
                      

                      no arguments are passed to "compareFunction", whilst requiring 2 arguments

                      bool compareFunction (std::string a, std::string b) {return a<b;} 
                      

                      thnx @J-Hilk and @JonB

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Natural_Bugger
                      compareFunction is passed to std::sort() without any following parentheses, i.e. not compareFunction(...). That means at this point is does not call the function, it merely passes the address of the function. std::sort() works through the vector element range you passed, taking pairs of elements and now calling compareFunction(element1, element2).

                      1 Reply Last reply
                      1

                      • Login

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