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

LVL 2
rickhanAsked:
Who is Participating?
 
rickhanAuthor Commented:
strUnsortedArray.OrderBy(b => b.Title).ToList();
This syntax seems to work fine, though--no idea why...suggestion from another blog..
0
 
käµfm³d 👽Commented:
"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

0
 
rickhanAuthor Commented:
Return strSmartListChecked.Sort(); gives me the same error msg as the array.sort did...
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
käµfm³d 👽Commented:
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?
0
 
rickhanAuthor Commented:
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.
0
 
käµfm³d 👽Commented:
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.
0
 
rickhanAuthor Commented:
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.
0
 
rickhanAuthor Commented:
Only solution came from a comment from another list.
You need a section on Windows Phone 7 programming, as it's getting more popular.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.