I'm having trouble with differences in the way a listbox sort its elements and the CompareTo-function.
The thing is, I am using two listboxes, and trying to make two lists of elements only occurring in one of them. Both listboxes are sorted using the sorted property.
My program runs through the listboxes, and compare the elements one by one, using the CompareTo-function:
if (listBox1.Items[x].ToString().CompareTo(listBox2.Items[y].ToString())) > 0 etc.
Now, everything works fine, except for items containing an apostrophe (') - like in "Donald's Pizza":
In a sorted listbox, "Donald's Pizza" comes before "Donald Duck". The apostrophe is less than space. But using the CompareTo-function, "Donald's Pizza" is greater than "Donald Duck" . "CompareTo" says, apostrophe is greater than space !
This messes up my system.
If I knew this was only the apostrophe that causes the problem, I could easily make a workaround, but now I'm not secure if it could apply to other characters too.
As a solution I´ll have to make my own sort-procedure to the listboxes, but am I just overlooking something obvious?
EDIT: Thanks for the answer.
I ended up making my own sort-procedure, based on the CompareTo-function. This way I'm sure the sort of the listbox is 100% equal to the CompareTo-function I use later.
public ListBox fn_sort_listbox(ListBox par_listbox)
{
ListBox lb_work = new ListBox();
int in_index;
int in_compare;
if (par_listbox.Items.Count == 0) return lb_work;
foreach (object i in par_listbox.Items)
{
in_index = 0;
while (in_index < lb_work.Items.Count)
{
in_compare = lb_work.Items[in_index].ToString().CompareTo(i.ToString());
if (in_compare > 0)
{
break;
}
in_index++;
}
lb_work.Items.Insert(in_index, i.ToString());
}
return lb_work;
}