Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem Sorting List
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01468908
Message ID:
01468920
Views:
33
>I have loaded a list with Windows Services:
>
>
>public static List<ServiceController> GetAllServices()
>{
>    ServiceController[] Temp = ServiceController.GetServices();
>    List<ServiceController> Services = new List<ServiceController>(Temp);
>    Services.Sort();
>
>    return Services;
>}
>
>
>The Sort line throws the error "Failed to compare two elements in the array." Google hits are obscure on this. Anyone know what this is about?

Generic Lists can only sort if the type being sorted implements IComparable or if you supply a comparer as a parameter. Try something like:
 private static int CompareService(ServiceController x, ServiceController y)
{
  return x.DisplayName.CompareTo(y.DisplayName);
}
then
Services.Sort(CompareService);
Previous
Reply
Map
View

Click here to load this message in the networking platform