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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
rickhanAuthor Commented:
strUnsortedArray.OrderBy(b => b.Title).ToList();
This syntax seems to work fine, though--no idea why...suggestion from another blog..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.