rickhan
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.
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!");
}
}
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?
ASKER
private List<MyCheckBox> sortList(List<MyCheckBox> strUnsortedArray)
strSmartListSorted.AddRang e(Array.So rt<MyCheck Box>(strSm artListChe cked));
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.
strSmartListSorted.AddRang
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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.
In you last post, the line:
strSmartListSorted.AddRange(Array.Sort<MyCheckBox>(strSmartListChecked));
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.
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.
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.
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.
You need a section on Windows Phone 7 programming, as it's getting more popular.
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:
Open in new window