[ad_1]
I have an array of Items, each one is a structure with an expiration date field (a string based on the pattern: dd/mm/yyyy).
I sorted the array with maxsort algorithm but came across a promblem when several Items had the same expiration date.
According to the exercise`s instructions, in the above-mentioned scenario the order between the Items should be by the order of their appearance in the presorted array.
Here is the code:
typedef struct _item {
char item_name[ITEM_NAME_LENGTH];
char department[DEPARTMENT_NAME_LENGTH];
char expiration_date[EXPIRATION_DATE_STRING_LENGTH];
double price;
int available;
}Item;
void swap(Item* xp, Item* yp)
{
Item temp = *xp;
*xp = *yp;
*yp = temp;
}
void Sort_By_Date(Item* store, int *len)
{
Item* temp_store = (Item*)malloc(*(len) * sizeof * store);
int length, i;
for (length = *(len); length > 1; --length)
{
int i_max = Index_of_max(store, length);
swap(&store[length - 1], &store[i_max]);
}
free(temp_store);
}
case 2:
{
Sort_By_Date(store, len);
printf("You chose to sort the inventory by the expiration date.\n");
Actions_Menu();
continue;
}
I thought about making a copy of the array before the sorting and then deal with the duplication problem, but couldn`t come up with the right solution.
thanks in advance!
[ad_2]