Hallo Leute,
eine List von einem benutzerdefinierten Objekt will sich nicht sortieren lassen.
Hier ein Code-Auszug:
der Fehler taucht bei
und bei
auf, dabei hatte ich ein Beispiel-Projekt in dem es genau so geschrieben wird und aus der Fehlermeldung
werd ich nicht so ganz schlau...
Vielleicht weis einer von Euch nen Rat oder sieht was ich da falsch mache. Es ist Freitag und das ist bei mir der "LustLosDay"
cu s00n Andralin
eine List von einem benutzerdefinierten Objekt will sich nicht sortieren lassen.
Hier ein Code-Auszug:
Code:
class Program
{
static List<my_object_class> my_object_list = new List<my_object_class>();
static void Main(string[] args)
{
my_object_list.Add(new my_object_class(1, "erster Eintrag", 100));
my_object_list.Add(new my_object_class(3, "dritter Eintrag", 50));
my_object_list.Add(new my_object_class(2, "zweiter Eintrag", 10));
foreach (my_object_class moc in my_object_list)
Console.WriteLine(moc.nr.ToString("000") + " | " + moc.datum + " | " + moc.wert.ToString("00000") + " | " + moc.text);
Console.WriteLine();
my_object_list.Sort(new sort_byNr());
foreach (my_object_class moc in my_object_list)
Console.WriteLine(moc.nr.ToString("000") + " | " + moc.datum + " | " + moc.wert.ToString("00000") + " | " + moc.text);
Console.WriteLine();
my_object_list.Sort(new sort_byWert());
foreach (my_object_class moc in my_object_list)
Console.WriteLine(moc.nr.ToString("000") + " | " + moc.datum + " | " + moc.wert.ToString("00000") + " | " + moc.text);
Console.WriteLine();
Console.WriteLine("Ende, Taste drücken");
Console.ReadKey();
}
}
public class my_object_class : IComparable
{
public int nr;
public string text;
public int wert;
public DateTime datum;
public my_object_class(int d_nr, string d_text, int d_wert)
{
nr = d_nr;
text = d_text;
wert = d_wert;
datum = DateTime.Now;
}
int IComparable.CompareTo(object obj)
{
my_object_class tmp_obj = (my_object_class)obj;
if (this.nr > tmp_obj.nr)
return (1);
else
return (-1);
}
}
public class sort_byNr : IComparer<Object>
{
public int Compare(object x, object y)
{
if (x == null && y == null) return 0;
if (x == null) return 1;
if (y == null) return -1;
if (x.GetType() != y.GetType())
throw new ArgumentException("Sort Error");
return ((my_object_class)x).nr.CompareTo(((my_object_class)y).nr);
}
}
public class sort_byWert : IComparer<Object>
{
public int Compare(object x, object y)
{
if (x == null && y == null) return 0;
if (x == null) return 1;
if (y == null) return -1;
if (x.GetType() != y.GetType())
throw new ArgumentException("Sort Error");
return ((my_object_class)x).wert.CompareTo(((my_object_class)y).wert);
}
}
Code:
my_object_list.Sort(new sort_byNr());
Code:
my_object_list.Sort(new sort_byWert());
Code:
Fehler 1 Die beste Übereinstimmung für die überladene System.Collections.Generic.List<MBS008.list.objects.my_object_class>.Sort(System.Collections.Generic.IComparer<MBS008.list.objects.my_object_class>)-Methode hat einige ungültige Argumente. P:\EigeneDateien\Visual Studio 2008\Projects\MBS008.list.objects\MBS008.list.objects\Program.cs 22 13 MBS008.list.objects
Vielleicht weis einer von Euch nen Rat oder sieht was ich da falsch mache. Es ist Freitag und das ist bei mir der "LustLosDay"
cu s00n Andralin