2

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;
    }
Mads Aggerholm
  • 432
  • 1
  • 5
  • 13
  • see http://social.msdn.microsoft.com/Forums/en-IN/csharpgeneral/thread/6d973127-e861-4852-b6e8-235607229e45 – Alvin Wong Jul 03 '12 at 03:43

1 Answers1

1

The example comparer on Microsoft's site compares in the same order as the ListBox. So the code below produces "Donald's Pizza" before "Donald Duck".

  string[] strings = new string[2] { "Donald Duck", "Donald's Pizza" };
  Array.Sort(strings, new MyStringComparer(CompareInfo.GetCompareInfo("en-US"),
      CompareOptions.StringSort));
  foreach (string item in strings)
    Console.WriteLine(item);

The MyStringComparer comes from here and is implemented as:

public class MyStringComparer : IComparer
{
  private CompareInfo myComp;
  private CompareOptions myOptions = CompareOptions.None;

  // Constructs a comparer using the specified CompareOptions.
  public MyStringComparer(CompareInfo cmpi, CompareOptions options)
  {
    myComp = cmpi;
    this.myOptions = options;
  }

  // Compares strings with the CompareOptions specified in the constructor.
  public int Compare(Object a, Object b)
  {
    if (a == b) return 0;
    if (a == null) return -1;
    if (b == null) return 1;

    String sa = a as String;
    String sb = b as String;
    if (sa != null && sb != null)
      return myComp.Compare(sa, sb, myOptions);
    throw new ArgumentException("a and b should be strings.");

  }
}
Michael
  • 8,891
  • 3
  • 29
  • 42
  • 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. – Mads Aggerholm Jul 06 '12 at 04:11