Link to home
Start Free TrialLog in
Avatar of rickhan
rickhanFlag for United States of America

asked on

C#:Silverlight:WP7 array.sort of custom list

I have a CheckBox custom class that I use to fill a listbox. That works fine, but I need to sort them by the title of each checkbox.
MyCheckBox class doesn't generate any errors, but trying to use an array.sort fails:
"Best overloaded method has some invalid arguments"

Using Visual Studio 2010 on a Windows Phone 7 app.
List<MyCheckBox> strSmartListChecked = new List<MyCheckBox>();
Return Array.Sort(strSmartListChecked); // doesn't like this syntax

public class MyCheckBox:IComparable
        {
            public MyCheckBox() { }
            public MyCheckBox(string title, bool ischecked)
            {
                Title = title;
                IsChecked = ischecked;
            }
            public string Title { get; set; }
            public bool IsChecked { get; set; }


             int IComparable.CompareTo(object obj)
            {
                MyCheckBox temp =  (MyCheckBox)obj;
                if (temp != null)
                    return this.Title.CompareTo(temp.Title);
                else throw new ArgumentException("Parameter is not a title of a checkbox!");
            }
        }

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

"Best overloaded method has some invalid arguments"

That's because you're trying to pass a generic list to a method that expects an array. The List<> class has a built in function called Sort(). Use that:
List<MyCheckBox> strSmartListChecked = new List<MyCheckBox>();

Return strSmartListChecked.Sort();

Open in new window

Avatar of rickhan

ASKER

Return strSmartListChecked.Sort(); gives me the same error msg as the array.sort did...
Your using "Return" to indicate you are sending this value back from a function. What the declaration (the first line of the function) of the function look like?
Avatar of rickhan

ASKER

private List<MyCheckBox> sortList(List<MyCheckBox> strUnsortedArray)
 strSmartListSorted.AddRange(Array.Sort<MyCheckBox>(strSmartListChecked));
 return strSmartListSorted;  // this works if I remove the Array.sort

The error also happens without using return.
I'm switching from coding in vb.net to c#.net, so I'm learning C# as I go.
ASKER CERTIFIED SOLUTION
Avatar of rickhan
rickhan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In my opinion, you don't need this function. Unless you want some custom sort order, you should be able to use the built in sort function I mentioned previously.

In you last post, the line:
strSmartListSorted.AddRange(Array.Sort<MyCheckBox>(strSmartListChecked));

Open in new window


will not work because Array.Sort() has a return type of void (think SUB in VB). My advice is to replace any calls you have to this function with calls to the sort method of List.
Avatar of rickhan

ASKER

Sort method of List doesn't seem to work with my custom list of MyCheckbox.
I do a lot more in the function than a simple sort -- the simple sort is the last step before combining all the list segments and passing back the result.
I needed a list sorted by ischecked and title, so all the checked are at top, sorted alphabetically, with the unchecked following, listed alphabetically.
There probably is a more elegant solution, but this one works.
Avatar of rickhan

ASKER

Only solution came from a comment from another list.
You need a section on Windows Phone 7 programming, as it's getting more popular.