Structure-Array nach bestimmter eigenschaft sortieren

  • VB.NET

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Structure-Array nach bestimmter eigenschaft sortieren

    Halllo Leute,

    wie kann ich die Array.Sort-Methode auf ein Structure-Array anwenden?
    Mein bisheriger Code:

    VB.NET-Quellcode

    1. Public Structure ColorProbabilityStrucure
    2. Implements IComparable(Of ColorProbabilityStrucure)
    3. Public Property Color As Drawing.Color
    4. Public Property Probability As Double
    5. Public Function CompareTo(ByVal other As ColorProbabilityStrucure) As Integer Implements System.IComparable(Of _
    6. ColorProbabilityStrucure).CompareTo
    7. Dim oY As ColorProbabilityStrucure = CType(other, ColorProbabilityStrucure)
    8. If Me.Probability > oY.Probability Then Return 1
    9. If Me.Probability < oY.Probability Then Return -1
    10. If Me.Probability = oY.Probability Then
    11. If Me.Color.ToArgb > oY.Color.ToArgb Then Return 1
    12. If Me.Color.ToArgb < oY.Color.ToArgb Then Return -1
    13. If Me.Color.ToArgb = oY.Color.ToArgb Then Return 0
    14. End If
    15. End Function
    16. End Structure
    Und in dem Sub Main möchte ich folgendes anwenden:

    VB.NET-Quellcode

    1. Dim _arr(20) As ColorProbabilityStrucure
    2. Array.Sort('<Code, um das Array nach der Property "Probability" zu sortieren>)
    Wie soll ich das anwenden?

    Vielen Dank,
    wincrash
    (\_/) Das ist Hase.
    (O.o) Kopiere Hase in deine Signatur
    (> <) und hilf ihm so auf seinem Weg zur Weltherrschaft.
    Hi
    möchtest du jetzt die bestehende IComparable-Implementierung verwenden oder manuell nach der Probability-Eigenschaft sortieren (btw. hast du Strucure geschrieben)?
    Wenn du nach IComparable sortieren willst, einfach Array.Sort(_arr), sonst Array.Sort(_arr, Function(l, r) l.Probability.CompareTo(r.Probability)).

    Gruß
    ~blaze~
    @Wincrash:: Oder so:
    IComparer

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private xx As New List(Of MyColor)
    3. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    4. xx.Sort(New MyColorComparer)
    5. End Sub
    6. End Class
    7. Public Class MyColor
    8. Public Property Color As Drawing.Color
    9. Public Property Probability As Double
    10. End Class
    11. Public Class MyColorComparer
    12. Implements IComparer(Of MyColor)
    13. Public Function Compare(x As MyColor, y As MyColor) As Integer Implements System.Collections.Generic.IComparer(Of MyColor).Compare
    14. If x.Probability > y.Probability Then Return 1
    15. If x.Probability < y.Probability Then Return -1
    16. If x.Probability = y.Probability Then
    17. If x.Color.ToArgb > y.Color.ToArgb Then Return 1
    18. If x.Color.ToArgb < y.Color.ToArgb Then Return -1
    19. If x.Color.ToArgb = y.Color.ToArgb Then Return 0
    20. End If
    21. Return 0
    22. End Function
    23. End Class
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ich habe letzten Endes die Lösung von RodFromGermany verwendet.
    Vielen Dank an alle,
    wincrash

    @~blaze~: Danke, dass du den Schreibfehler gefunden hast :thumbup:
    (\_/) Das ist Hase.
    (O.o) Kopiere Hase in deine Signatur
    (> <) und hilf ihm so auf seinem Weg zur Weltherrschaft.