Utilisation de l'interface IComparer
Bonjour,
Dans une macro, j'utilise des ArrayList qui contiennent des objets de la classe Match.
J'aimerai utiliser la méthode Sort(IComparer) afin de trier comme je veux cette liste, mais voila l'interface IComparer est introuvable lors de l'appel dans MatchComparer (Implements).
Comment puis-je trier ma liste facilement?
Merci de votre aide.
Voici mon code:
Module de classe : Match
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| Option Explicit
Private matchDate As Date
Private matchLine As Long
Public Property Let setMatchDate(matchDateNew As Date)
matchDate = matchDateNew
End Property
Public Property Get getMatchDate() As Date
getMatchDate = matchDate
End Property
Public Property Let setMatchLine(matchLineNew As Long)
matchLine = matchLineNew
End Property
Public Property Get getMatchLine() As Long
getMatchLine = matchLine
End Property
Public Property Get toString() As String
toString = CStr(matchDate) + "//" + CStr(matchLine)
End Property |
Module de classe : MatchComparer
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Option Explicit
Implements IComparer
Private Function IComparer_Compare(match1 As match, match2 As match) As Long
If match1.getMatchDate > match2.getMatchDate Then
IComparer_Compare = -1
End If
If match1.getMatchDate < match2.getMatchDate Then
IComparer_Compare = 1
End If
If match1.getMatchDate = match2.getMatchDate Then
IComparer_Compare = 0
End If
End Function |
Module : Module2
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| Option Explicit
Sub test()
Dim listeMatchs As Object
Set listeMatchs = CreateObject("System.Collections.ArrayList")
Dim match1 As match
Dim match2 As match
Dim match3 As match
Set match1 = New match
match1.setMatchDate = CDate("30/12/2014 11:00")
match1.setMatchLine = 1
Set match2 = New match
match2.setMatchDate = CDate("29/12/2014 11:00")
match2.setMatchLine = 2
Set match3 = New match
match3.setMatchDate = CDate("31/12/2014 11:00")
match3.setMatchLine = 3
listeMatchs.Add match1
listeMatchs.Add match2
listeMatchs.Add match3
listeMatchs.Sort (New MatchComparer)
Dim element As match
For Each element In listeMatchs
MsgBox element.toString
Next element
End Sub |