Mar 7, 2009 at 7:57pm UTC
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 Mar 7, 2009 at 8:51pm UTC
Mar 7, 2009 at 9:09pm UTC
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 Mar 7, 2009 at 9:11pm UTC
Mar 7, 2009 at 9:19pm UTC
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 Mar 7, 2009 at 9:22pm UTC
Mar 7, 2009 at 9:25pm UTC
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 Mar 7, 2009 at 9:25pm UTC
Mar 7, 2009 at 9:38pm UTC
kool lol thanks
right now its sorting wearied
I Enter: Output:
dog -------------dog
---------------------------
steven ----------- dog
---------------------steven
----------------------------
zelp--------------- steven
-------------------- zelp
---------------------dog
Last edited on Mar 7, 2009 at 9:39pm UTC
Mar 7, 2009 at 9:40pm UTC
Where are you storing it?
I am not sure which string are you trying to sort.
Mar 7, 2009 at 10:01pm UTC
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