sorting and comparing

so i made this sort and i have to sort it by a string so i made my own compair
but im having trouble with the compair function


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
void LinkList::sort()
 {
      node *temp, *temp2;
      temp = temp2 = starting;
      
      if(count() == 1)
      {
                 cout << "only one to sort" << endl;
                 return;
                 }
      
      while(temp->next->next !=NULL)
      {
                             while(temp2->next !=NULL)
                             {
                                         if(compair(temp2->y.Class,temp2->next->y.Class) == -1)
                                         {
                                                                 data holder = temp2->y;
                                                                 temp2->y = temp2->next->y;
                                                                 temp2->next->y = holder;                            
                                                                                              }
                                         temp2 = temp2->next;
                                              }
                             temp = temp->next;
                             }
      
      }
      
int LinkList::compair(string one,string two)
{
    char a[] = one;
    char b[] = two;
    int max;
    
    if(one.size() < two.size())
                   max = one.size();
    else
        max = two.size();
        
     for(int i = 0; i < max; i++)
     {
             if(a[i] < b[i])
             {
                         return -1;
                         }
             } 
      return 1;
}      


i would be happy with any compair function that i didn't know about built in im just trying to alphabetize my link list

well the error here is that the char arrays are throwing a LinkList.h initializer fails to determine size of `a' (dues the same for b)

than if i do set it with one.size() it still don't work lul and says i didnt inish the array at all
Last edited on
In compair(string one, string two) function use this code
1
2
3
4
5
char a[20];
char b[20];

strcpy(a,one.c_str());
strcpy(b,two.c_str()); 


Hope this helps !
it dose help but im not sure how long the strings will be but most likely less than 20 so ill call it acceptable for now xD

but i think i may have some thing wrong because its not ordering properly so im wondering if i did some thing wrong there


its not even doing it in reverse order but just all out of order
Last edited on
Well if you don't want to assign the size of the array, then don't. You can always do dynamic allocation.
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
string one = "One";
string two = "one";

char *a = new char[one.size()]; 
char *b = new char[two.size()]; 

strcpy(a,one.c_str()); 
strcpy(b,two.c_str());


int maxSize = one.size() < two.size() ? two.size() : one.size();

for (int i =0 ;i<maxSize; i++) {

     if ((int)a[i] < (int)b[i]) {
       continue; 
     }   
     else {

        return -1; 
     }   
}

delete [] a;
delete [] b;  
return 0;


Last edited on
but i think i may have some thing wrong because its not ordering properly so im wondering if i did some thing wrong there


Which string are you trying to reverse or sort in order?
String one or String two?
Last edited on
kool lol thanks

right now its sorting wearied

I Enter: Output:
dog -------------dog
---------------------------
steven ----------- dog
---------------------steven
----------------------------
zelp--------------- steven
-------------------- zelp
---------------------dog
Last edited on
Where are you storing it?
I am not sure which string are you trying to sort.
well im sorting the string stored in temp2->y.Class

Class is a string stored in a struct thats stored in my node structs

what im trying to do is switching around the data in the nodes to sort
with a bit of tinkering i figured it out and heres my product incase some one else decideds they need some thing similar


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
54
55
56
void LinkList::sort()
 {
      node *temp, *temp2;
      temp = temp2 = starting;
      
      if(count() == 1)
      {
                 cout << "only one to sort" << endl;
                 return;
                 }
      
      while(temp->next !=NULL)
      {                      temp2=starting;
                             while(temp2->next !=NULL)
                             {
                                         if(compair(temp2->y.Class,temp2->next->y.Class) == -1)
                                         {
                                                                 data holder = temp2->y;
                                                                 temp2->y = temp2->next->y;
                                                                 temp2->next->y = holder;                            
                                                                                              }
                                         temp2 = temp2->next;
                                         show();
                                              }
                             show();        
                             temp = temp->next;
                             }
      
      }
      
int LinkList::compair(string one,string two)
{
    
    char *a = new char[one.size()]; 
    char *b = new char[two.size()]; 
    
    strcpy(a,one.c_str()); 
    strcpy(b,two.c_str());
    
    
    int maxSize = one.size() < two.size() ? two.size() : one.size();
    
    for (int i =0 ;i<maxSize; i++) {
    
         if ((int)a[i] < (int)b[i]) {
           return 1; 
         }   
         if((int)a[i] > (int)b[i]) {
            return -1; 
         }   
    }
    
    delete [] a;
    delete [] b;  
    return 0;
}      


Topic archived. No new replies allowed.