List sorting -- how to compare strings?

Hello, I'm having a bit of trouble on sorting a list of multiple elements, based on the full name of a person.

The structure upon which the list is made:
1
2
3
4
5
6
7
struct ListSbj
{
    string name;
    string surname;

    struct ListSbj *next;
};


All pointers:
struct ListSbj *ListDS=0, *nd=0, *temp=0, *prev=0, *prev2=0;

And the sorting part of the code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
do {
        comp = 0;//Using integers for the bubble sorting
        if(ListDS)//Checking the existence of the head pointer
        {
            prev = ListDS;
            if(prev->next)//Checking the existence of a second element on the list
            {
                nd = prev->next;
                if(prev->surname > nd->surname)//comparison of the first two elements
                {
                    comp++;
                    temp = prev->next;
                    prev->next = nd->next;
                    nd->next = ListDS;
                    ListDS = temp;
                }
                else if(prev->name > nd->name)
                {
                    comp++;
                    temp = prev->next;
                    prev->next = nd->next;
                    nd->next = ListDS;
                    ListDS = temp;
                }
                else {}

                while(nd->next)//Checking for any next element
                {
                    prev2 = prev;//setting pointers
                    prev = nd;
                    nd = nd->next;

                    if(prev->surname > nd->surname)//comparison of n-th and (n+1)-th elements
                    {
                        comp++;
                        temp = prev->next;
                        prev->next = nd->next;
                        nd->next = prev2->next;
                        prev2->next = temp;
                    }
                    else if(prev->name > nd->name)
                    {
                        comp++;
                        temp = prev->next;
                        prev->next = nd->next;
                        nd->next = prev2->next;
                        prev2->next = temp;
                    }
                    else {}
                }
            }
        }
    } while (comp); //Checking if comparisons have been executed 


I expect any number of elements on the list, thus the checks on the head, as well as the first two elements of the list.

The thing is that the code is running without any errors popping up, although the comparison of the strings just doesn't happen at all. What can I do?

Edit: Removed the ! at coder777's pointing
Last edited on
Line 3: It's a check if it doesn't exists (i.e. ListDS == NULL). Remove the !
Checked that today, I am cringing at that!

However, the sorting still doesn't happen; I print the list both before and after the sorting, and the second printing doesn't happen at all.
It isn't bubble sort. It would require two nested loop:
1
2
3
4
5
6
7
8
9
10
bool requires_sort;
do
{
  requires_sort = false;
  nd = ListDS;
  while(nd->next)
  {
...
  }
} while(requires_sort);
Set requires_sort to true if you actually change the order.

I would guess that only if surname is equal you need to check name like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                    if(prev->surname > nd->surname)
                    {
requires_sort = true;
                    }
                    else if((prev->surname == nd->surname) && (prev->name > nd->name))
                    {
requires_sort = true;
                    }
                    else {}
if(requires_sort)
{
  if(0 == comp)
     // ListDS
  else
     // rest
++comp;
}
Thanks for the help, I spotted more syntax mistakes on the code, such as the case of the missing surname equality arguement when comparing the names.

I'll do a different code, where I sort list entries as they are added on the list.

Thank you for your help!
Topic archived. No new replies allowed.