Help with multi-sort
-
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
-
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
FarPointer wrote:
Will this Suffice
No, because
qsort()
is not a stable sort. You would need to combine the effects ofqsort_callbackName()
andqsort_callbackAge()
and callqsort()
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
-
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 -
FarPointer wrote:
Will this Suffice
No, because
qsort()
is not a stable sort. You would need to combine the effects ofqsort_callbackName()
andqsort_callbackAge()
and callqsort()
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
Wat do you mean by not a stable sort ,it should be a well tested and implemented one right(IMHO). Regards, FarPointer
-
FarPointer wrote:
Will this Suffice
No, because
qsort()
is not a stable sort. You would need to combine the effects ofqsort_callbackName()
andqsort_callbackAge()
and callqsort()
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
-
Wat do you mean by not a stable sort ,it should be a well tested and implemented one right(IMHO). Regards, FarPointer
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
-
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
Well thanks for making it clear . Regards, FarPointer
-
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
-
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, 2006FarPointer 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
-
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
Neat and Clean. Regards, FarPointer
-
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