Answer the question
In order to leave comments, you need to log in
Merge sort on multiple conditions?
I need merge sort by name and sum to sort database elements. When I sort only by name or only by sum, everything works fine, but together these conditions do not give the desired result. The elements change their order, but not completely as they should. What is wrong I can not understand. I want to get all the names in alphabetical order for a smaller amount and so on for the rest to the end, and now I get what is in the image
struct human // ya4eika dlya bazi
{
char name[32] = { 0 };
unsigned short int number;
char date[8] = { 0 };
char lawyer[22] = { 0 };
} *stacks;
struct lister // spisok
{
lister* next;
human* data;
} *head, * tail;
int compUsers(lister* first, lister* second)
{
int nameCompareResult = strcmp(first->data->name, second->data->name);
if (nameCompareResult)
return nameCompareResult;
else
return first->data->number - second->data->number;
}
lister* mergeSortedList(lister* stl1, lister* stl2)
{
lister* result = NULL;
if (stl1 == NULL)
return stl2;
if (stl2 == NULL)
return stl1;
int num = strncmp(stl1->data->name, stl2->data->name, 3);
//recursing merging 2 lists
if (compUsers(stl1, stl2) < 0) //(stl1->data->number <= stl2->data->number)//(num <= 0)
{
result = stl1;
result->next = mergeSortedList(stl1->next, stl2);
}
else
{
result = stl2;
result->next = mergeSortedList(stl1, stl2->next);
}
return result;
}
void mergeS(lister** thead)
{
lister* head = *thead;
lister* ptr1;
lister* ptr2;
if (head == NULL || head->next == NULL)
return;
splitList(head, &ptr1, &ptr2);
//recursively sorting 2 lists
mergeS(&ptr1);
mergeS(&ptr2);
*thead = mergeSortedList(ptr1, ptr2);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question