Double Sorting
-
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; };
-
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; };
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"
-
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; };
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