Problem With Sort

G'Day All,

Please excuse me if this turns out to be a duplicate question.

After a day of trying to figure out why Sort did not work, I finally realized the capital S was the problem. The sort finally worked, but the output was not hat I expected. Sorting on then "Name" value of a (struct) is not giving me the alphabetical listing that I want or expected.

For reference the struct I am using is:

struct DVD
{
char Name[60];
int Catagory;
int RunTime;
int Rating;
};

The "char Name[60];" started out as "string Name;" until I gave up trying to figure out how to increase the capacity of the string. The string worked until I wrote and then read from a file. The string only had a capacity of 15.
In the main function I have the declaration of "vector<DVD> DvdList(10)" which is working for everything I have used it for.

The sort function call "SortByName" that originally looked this way:

bool SortByName(const DVD &lhs, const DVD &rhs) { return lhs.Name < rhs.Name; }

This worked, but not well. I tried something different that worked a little better:

bool SortByName(const DVD &lhs, const DVD &rhs)
{
int Result = strcmp(lhs.Name, rhs.Name);
if (Result == -1 || Result == 0)
{
return true;
}
return false;
}

Worked better, but not right.

My last attempt was to use "stable_sort". I think this might have made a slight difference, but not sure. Here is what I am trying to sort the numbers are just line numbers for now and the first line is the "sort" code. Count is a variable to keep track of the number of entries that I have made:

stable_sort(DvdList.begin(), DvdList.begin() + Count, SortByName);

0, Chloe
1, Celtic Woman Believe
2, Celtic Woman The Greatest Jounery Essentiaal Collection
3, The Hobbit
4, Celtic Woman A New Journey
5, Startrek Voyager Season 3
6, Startrek Voyager Season 4
7, CSI NY Season 1

And what I get with the second "SortByName" is:

0, CSI NY Season 1
1, Celtic Woman A New Journey
2, Celtic Woman Believe
3, Celtic Woman The Greatest Jounery Essentiaal Collection
4, Chloe
5, Startrek Voyager Season 3
6, Startrek Voyager Season 4
7, The Hobbit

Somewhat better, but not exactly correct.

I used the "DvdList.begin() + Count" instead of "the ".end()" thinking that having a vector size of ten with eight entries might have been a problem.

Any advise or suggestions you may have will be appreciated.

Thanks,
Andy
The "char Name[60];" started out as "string Name;" until I gave up trying to figure out how to increase the capacity of the string.

The string takes care of its own capacity. You just add to it and it grows as necessary without you having to worry about it. If something went wrong when you tried to write/read to file, it's not anything to do with the string's capacity. I advise you to go back to using proper C++ strings. Using char arrays is opening yourself up to a large set of extra work, with lots of opportunities for errors.


The second "SortByName" , as you surmise, is case sensitive. If you want it to be case-insensitive, one option would be to could create a completely lowercase ( see function tolower ) copy of the strings in your comparison function, and then compare those copies.
Last edited on
Moschops, Thank you.

The "string Name" problem was only in the write to file function, everywhere else in the program the "string Name" worked fine. Something to work on in the future.

The case sensitivey problem was with the call to the "sort" function, I started with a capital letter not the lower case "s" I needed. Changing the "SortByName" to use all lower case letters in the "strcmp" function did make all the difference and so far it is working correctly. Still need to do some more testing though. Tried using "_strcmpi" function, but that did not work.

For now working better. Thank you for all your help.

Andy
_strcmpi is a non-standard function and will only work on particular compilers on particular systems. They're a bad habit to get into.

Topic archived. No new replies allowed.