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. Double Sorting

Double Sorting

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialalgorithmshelp
3 Posts 3 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 Offline
    K Offline
    Kurt _B
    wrote on last edited by
    #1

    Need help on sorting a CArray of structs by 2 values. An example of a similar struct is below, say I wanted to sort by name & then age. I have a quick sort function that can sort one a single value, by I am clueless as in how to do two. Any help would be greatly appreciated. Kurt struct Person { CString name; int age; };

    R K 2 Replies Last reply
    0
    • K Kurt _B

      Need help on sorting a CArray of structs by 2 values. An example of a similar struct is below, say I wanted to sort by name & then age. I have a quick sort function that can sort one a single value, by I am clueless as in how to do two. Any help would be greatly appreciated. Kurt struct Person { CString name; int age; };

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      It's actually quite easy. I assume you're using a comparison method like the one used by qsort(). The function returns +ve if the first argument belongs before the second, -ve if the second belongs before the first, and 0 if the two are considered equal:

      int comparePersons(void *arg1, void *arg2)
      {
      Person *person1 = (Person*)arg1;
      Person *person2 = (Person*)arg2;

      if (person1->name < person2->name)
      return 1;
      else if (person1->name > person2->name)
      return -1;
      else
      {
      // The name is the same so sort by age
      if (person1->age < person2->age)
      return 1;
      else if (person1->age > person2->age)
      return -1;
      else
      return 0;
      }
      }

      Of course, you can change the < and > around to reverse the sort order if you need to.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      1 Reply Last reply
      0
      • K Kurt _B

        Need help on sorting a CArray of structs by 2 values. An example of a similar struct is below, say I wanted to sort by name & then age. I have a quick sort function that can sort one a single value, by I am clueless as in how to do two. Any help would be greatly appreciated. Kurt struct Person { CString name; int age; };

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #3

        Try this... #include #include #include #include using namespace std; struct Person { Person(const CString& n = "", int a = 0) : name(n), age(a) {} CString name; int age; }; class ComparePerson : public binary_function { public: // Sort by name and then age bool operator()(const Person& first, const Person& second) const { return first.name < second.name || (first.name == second.name && first.age < second.age); } }; void SortTest() { CArray person; person.Add(Person("Kevin", 30)); person.Add(Person("Gary", 24)); person.Add(Person("Peter", 36)); person.Add(Person("Peter", 16)); person.Add(Person("Richard", 19)); person.Add(Person("Joanne", 40)); int count = person.GetSize(); afxDump << "Initial contents\n"; for (int i = 0; i < count; i++) { afxDump << "name = " << person[i].name << ", " << "age = " << person[i].age << "\n"; } // Last Person Person* begin = person.GetData(); Person* end = begin + count; afxDump << "Last Person \n"; Person* last = max_element(begin, end, ComparePerson()); afxDump << "Last Person: " << "name = " << last->name << ", " << "age = " << last->age << "\n"; afxDump << "\n"; // Sort sort(begin, end, ComparePerson()); afxDump << "Sorted persons\n"; for (i = 0; i < count; i++) { afxDump << "name = " << person[i].name << ", " << "age = " << person[i].age << "\n"; } } Output Sorted persons name = Gary, age = 24 name = Joanne, age = 40 name = Kevin, age = 30 name = Peter, age = 16 name = Peter, age = 36 name = Richard, age = 19 Is this what you want? Kevin

        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