Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with multi-sort

Help with multi-sort

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
13 Posts 4 Posters 0 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.
  • K Kurt _B

    I have a CArray of structs and I would like to sort my multiple members of the struct. Say it's a person struct and I want to sort by last name & age. Or maybe last name, first name, and age. Anyone have a clue how to do this? Thanks Kurt

    F Offline
    F Offline
    FarPointer
    wrote on last edited by
    #2

    Hi, Will this Suffice , then implement the callback // call the functions: First sort with name qsort(qsortdata, count, sizeof(struct Person), qsort_callbackName); // then sort the required ones age etc qsort(qsortdata, count-noidfiles, sizeof(struct Person), qsort_callbackAge); (IMHO) Regards, FarPointer -- modified at 13:06 Friday 31st March, 2006

    K D 2 Replies Last reply
    0
    • F FarPointer

      Hi, Will this Suffice , then implement the callback // call the functions: First sort with name qsort(qsortdata, count, sizeof(struct Person), qsort_callbackName); // then sort the required ones age etc qsort(qsortdata, count-noidfiles, sizeof(struct Person), qsort_callbackAge); (IMHO) Regards, FarPointer -- modified at 13:06 Friday 31st March, 2006

      K Offline
      K Offline
      Kurt _B
      wrote on last edited by
      #3

      What do you mean by this in the second sort? count-noidfiles

      F 1 Reply Last reply
      0
      • F FarPointer

        Hi, Will this Suffice , then implement the callback // call the functions: First sort with name qsort(qsortdata, count, sizeof(struct Person), qsort_callbackName); // then sort the required ones age etc qsort(qsortdata, count-noidfiles, sizeof(struct Person), qsort_callbackAge); (IMHO) Regards, FarPointer -- modified at 13:06 Friday 31st March, 2006

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        FarPointer wrote:

        Will this Suffice

        No, because qsort() is not a stable sort. You would need to combine the effects of qsort_callbackName() and qsort_callbackAge() and call qsort() only once.


        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

        "There is no death, only a change of worlds." - Native American Proverb

        F K 2 Replies Last reply
        0
        • K Kurt _B

          What do you mean by this in the second sort? count-noidfiles

          F Offline
          F Offline
          FarPointer
          wrote on last edited by
          #5

          Well what iam actually doing is that first i will sort with respect to the name ,then sort the sorteddata with respect to the age . int qsort_callback(const void * first, const void * second) { struct person *p1, * p2; p1 = (struct person *)first; p2 = (struct person *)second; //According to your preference if( (*(p1->Name)).Compare(*(p2->Name)) >0 ) { //for the age if(p1->Age > p2->Age) return 1; if(p1->Age < p2->Age) return -1; } //else same way as above } // Well here i have tried to make sorting in one go as David says qsort is not stable one but the previous one is more logical clear ,like if later we need a more customization on the sort you again sort on the sorted data Regards, FarPointer -- modified at 13:20 Friday 31st March, 2006

          D 1 Reply Last reply
          0
          • D David Crow

            FarPointer wrote:

            Will this Suffice

            No, because qsort() is not a stable sort. You would need to combine the effects of qsort_callbackName() and qsort_callbackAge() and call qsort() only once.


            "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

            "There is no death, only a change of worlds." - Native American Proverb

            F Offline
            F Offline
            FarPointer
            wrote on last edited by
            #6

            Wat do you mean by not a stable sort ,it should be a well tested and implemented one right(IMHO). Regards, FarPointer

            D 1 Reply Last reply
            0
            • D David Crow

              FarPointer wrote:

              Will this Suffice

              No, because qsort() is not a stable sort. You would need to combine the effects of qsort_callbackName() and qsort_callbackAge() and call qsort() only once.


              "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

              "There is no death, only a change of worlds." - Native American Proverb

              K Offline
              K Offline
              Kurt _B
              wrote on last edited by
              #7

              David Can you show me an example?

              D 1 Reply Last reply
              0
              • F FarPointer

                Wat do you mean by not a stable sort ,it should be a well tested and implemented one right(IMHO). Regards, FarPointer

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                Stable sorting algorithms maintain the relative order of records with equal keys. If whenever there are two records A and B with the same key and with A appearing before B in the original list, A will appear before B in the sorted list, the algorithm is said to be stable.


                "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                "There is no death, only a change of worlds." - Native American Proverb

                F M 2 Replies Last reply
                0
                • D David Crow

                  Stable sorting algorithms maintain the relative order of records with equal keys. If whenever there are two records A and B with the same key and with A appearing before B in the original list, A will appear before B in the sorted list, the algorithm is said to be stable.


                  "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                  "There is no death, only a change of worlds." - Native American Proverb

                  F Offline
                  F Offline
                  FarPointer
                  wrote on last edited by
                  #9

                  Well thanks for making it clear . Regards, FarPointer

                  1 Reply Last reply
                  0
                  • K Kurt _B

                    David Can you show me an example?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    int bynameinage( const void * v1, const void * v2 )
                    {
                    pmyData data1 = *(pmyData *) v1;
                    pmyData data2 = *(pmyData *) v2;

                    int dt = data1->age - data2->age;
                    
                    if (dt != 0)
                        return dt;
                    
                    // same age so check name
                    return \_tcscmp(data1->name, data2->name);    
                    

                    }
                    ...
                    qsort(..., bynameinage);


                    "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                    "There is no death, only a change of worlds." - Native American Proverb

                    F 1 Reply Last reply
                    0
                    • F FarPointer

                      Well what iam actually doing is that first i will sort with respect to the name ,then sort the sorteddata with respect to the age . int qsort_callback(const void * first, const void * second) { struct person *p1, * p2; p1 = (struct person *)first; p2 = (struct person *)second; //According to your preference if( (*(p1->Name)).Compare(*(p2->Name)) >0 ) { //for the age if(p1->Age > p2->Age) return 1; if(p1->Age < p2->Age) return -1; } //else same way as above } // Well here i have tried to make sorting in one go as David says qsort is not stable one but the previous one is more logical clear ,like if later we need a more customization on the sort you again sort on the sorted data Regards, FarPointer -- modified at 13:20 Friday 31st March, 2006

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      FarPointer wrote:

                      ...but the previous one is more logical clear ,like if later we need a more customization on the sort you again sort on the sorted data

                      For reasons already mentioned, calling qsort() again on the sorted list using a different key will not work.


                      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                      "There is no death, only a change of worlds." - Native American Proverb

                      1 Reply Last reply
                      0
                      • D David Crow

                        int bynameinage( const void * v1, const void * v2 )
                        {
                        pmyData data1 = *(pmyData *) v1;
                        pmyData data2 = *(pmyData *) v2;

                        int dt = data1->age - data2->age;
                        
                        if (dt != 0)
                            return dt;
                        
                        // same age so check name
                        return \_tcscmp(data1->name, data2->name);    
                        

                        }
                        ...
                        qsort(..., bynameinage);


                        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                        "There is no death, only a change of worlds." - Native American Proverb

                        F Offline
                        F Offline
                        FarPointer
                        wrote on last edited by
                        #12

                        Neat and Clean. Regards, FarPointer

                        1 Reply Last reply
                        0
                        • D David Crow

                          Stable sorting algorithms maintain the relative order of records with equal keys. If whenever there are two records A and B with the same key and with A appearing before B in the original list, A will appear before B in the sorted list, the algorithm is said to be stable.


                          "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                          "There is no death, only a change of worlds." - Native American Proverb

                          M Offline
                          M Offline
                          Maximilien
                          wrote on last edited by
                          #13

                          Thanks for the refresher course David. :)


                          Maximilien Lincourt Your Head A Splode - Strong Bad

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups