Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Problem Sorting List
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01468908
Message ID:
01468920
Vues:
31
>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);
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform