struct transaction * sortedArraysCommonElements(struct transaction *A, int ALen, struct transaction *B, int BLen) { int index1 = 0, index2 = 0, resindex = 0, compare; date d1, d2; if (A == NULL || B == NULL) return NULL; struct transaction *result = (struct transaction *)malloc(sizeof(struct transaction)*(ALen<BLen?ALen:BLen)); while ((index1 < ALen) && (index2 < BLen)) { getDayMonthAndYear(A[index1].date, &d1); getDayMonthAndYear(B[index2].date, &d2); compare = compareDate(d1, d2); if (compare == 1) index2++; else if (compare == -1) index1++; else { result[resindex++] = A[index1++]; index2++; } } if (resindex == 0) return NULL; return result; }
int TaskCompare::compare(Task *const &pt1, Task *const &pt2) { // result's intent is to catch a definitive: less than or more // it is important for sortable containers to avoid zero (0) // as zero represents equality //TODO: review for means of generic extensible implementation int result = 0; int sortLvl = 0; do { switch(m_pFields[sortLvl]) { case COMPARE_TASK_ID: result = pt1->CompareTo(*pt2); break; case COMPARE_DUE_DATE: result = compareDate(pt1->GetDueDate(), pt2->GetDueDate()); break; case COMPARE_PRIORITY: result = compareInt(pt1->GetPriority(), pt2->GetPriority()); break; //case COMPARE_PERCENT_COMPLETE: // result = compareInt(pt1->GetPercentComplete(), pt2->GetPercentComplete()); // break; case COMPARE_CREATE_DATE: result = compareDate(pt1->GetCreatedDate(), pt2->GetCreatedDate()); break; case COMPARE_ESTIMATED_DURATION: result = compareDate(pt1->GetEstimatedDuration(), pt2->GetEstimatedDuration()); break; case COMPARE_START_DATE: result = compareDate(pt1->GetScheduledStart(), pt2->GetScheduledStart()); break; case COMPARE_TASK_TITLE: result = compareText(pt1->GetTitle(), pt2->GetTitle()); break; default: break; } ++sortLvl; } while(!result && sortLvl < m_numSortLvls); if(!result)result = pt1->CompareTo(*pt2); return result; }