VB.NET, list(of T) et sort
Bonsoir,
Nouvelle problématique:
J'ai une classe "personne" qui inclut les éléments suivants:
Code:
1 2 3 4 5 6 7
| Public Class ClassPersonne
Public nom As String
Public prenom As String
Public age As Integer
Public identifiant As String
Public departement As String
End Class |
Je manipule ensuite un objet :
Code:
dim employes as list(of classPersonne)
Je souhaiterais utiliser la fonction sort sur cette liste du type "employes.sort"
Jusque là tout fonctionne...
Le problème est que je souhaiterais effectuer le sort sur l'identifiant. Apparemment, il est possible de définir dans la classe ce qui doit servir de clé de tri avec la syntaxe "comparer" mais je ne comprends pas les exemples fournis ni les différentes explications du site de Microsoft.
Quelqu'un peut m'aider ? Me proposer une syntaxe pour ma classe ?
Merci d'avance
sort d' objets personnalises dan une liste
bonjour mouzafc
Tu implemente simplement IComparable sur ta classe quand il s'agit de comparer des prop de type String.Pour des nombres c'est un peu plus complexe
Voici le code de ta classe personne repris en 2 exemples illustratifs simples:
-le premier fait le "sort" ou tri sur la prop Identifiant
code 1er exemple avec un dgv pour visualiser le resultat:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
'classe sortable sur "prop Identifiant"
Public Class ClassPersoComparableId
Implements IComparable
Private m_nom As String
Public Property Nom() As String
Get
Return m_nom
End Get
Set(ByVal value As String)
m_nom = value
End Set
End Property
Private m_prenom As String
Public Property Prenom() As String
Get
Return m_prenom
End Get
Set(ByVal value As String)
m_prenom = value
End Set
End Property
Private m_age As Integer
Public Property Age() As Integer
Get
Return m_age
End Get
Set(ByVal value As Integer)
m_age = value
End Set
End Property
Private m_identifiant As String
Public Property Identifiant() As String
Get
Return m_identifiant
End Get
Set(ByVal value As String)
m_identifiant = value
End Set
End Property
Private m_departement As String
Public Property Departement() As String
Get
Return m_departement
End Get
Set(ByVal value As String)
m_departement = value
End Set
End Property
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Dim c As ClassPersoComparableId = CType(obj, ClassPersoComparableId)
Return String.Compare(Me.Identifiant, c.Identifiant)
End Function
End Class
'forme du sortable sur "prop Identifiant"
Public Class frmPersoIdentifiant
Private perso1 As ClassPersoComparableId
Private employes1 As List(Of ClassPersoComparableId)
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub btnCreeListe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreeListe.Click
Dim rnd As Random = New Random
employes1 = New List(Of ClassPersoComparableId)
For i As Integer = 0 To 49
perso1 = New ClassPersoComparableId
perso1.Nom = "Quidam" & rnd.Next(1, 100).ToString
perso1.Prenom = "Prenom" & rnd.Next(1, 100).ToString
perso1.Identifiant = "ID" & rnd.Next(200, 400).ToString
perso1.Age = rnd.Next(25, 60)
perso1.Departement = rnd.Next(10, 95).ToString
employes1.Add(perso1)
Next
DataGridView1.DataSource = employes1
'on mets "colonne identifiant " en 1ere colonne pour mieux le voir
DataGridView1.Columns(3).DisplayIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortByIdentifiant.Click
employes1.Sort()
DataGridView1.DataSource = employes1
DataGridView1.Refresh()
End Sub
End Class |
-le 2eme fait le "sort" ou tri sur la prop Departement Identifiant
code 1er exemple avec un dgv pour visualiser le resultat:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
'classe sortable sur "prop Departement"
Public Class ClassPersoComparableDept
Implements IComparable
Private m_nom As String
Public Property Nom() As String
Get
Return m_nom
End Get
Set(ByVal value As String)
m_nom = value
End Set
End Property
Private m_prenom As String
Public Property Prenom() As String
Get
Return m_prenom
End Get
Set(ByVal value As String)
m_prenom = value
End Set
End Property
Private m_age As Integer
Public Property Age() As Integer
Get
Return m_age
End Get
Set(ByVal value As Integer)
m_age = value
End Set
End Property
Private m_identifiant As String
Public Property Identifiant() As String
Get
Return m_identifiant
End Get
Set(ByVal value As String)
m_identifiant = value
End Set
End Property
Private m_departement As String
Public Property Departement() As String
Get
Return m_departement
End Get
Set(ByVal value As String)
m_departement = value
End Set
End Property
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Dim c As ClassPersoComparableDept = CType(obj, ClassPersoComparableDept)
Return String.Compare(Me.Departement, c.Departement)
End Function
End Class
'forme du sortable sur "prop Departement"
Public Class frmPersoDepartement
Private perso2 As ClassPersoComparableDept
Private employes2 As List(Of ClassPersoComparableDept)
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
Private Sub btnCreeListe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreeListe.Click
Dim rnd As Random = New Random
employes2 = New List(Of ClassPersoComparableDept)
For i As Integer = 0 To 49
perso2 = New ClassPersoComparableDept
perso2.Nom = "Quidam" & rnd.Next(1, 100).ToString
perso2.Prenom = "Prenom" & rnd.Next(1, 100).ToString
perso2.Identifiant = "ID" & rnd.Next(200, 400).ToString
perso2.Age = rnd.Next(25, 60)
perso2.Departement = rnd.Next(10, 95).ToString
employes2.Add(perso2)
Next
DataGridView1.DataSource = employes2
'on mets "colonne departement" en 1ere colonne pour mieux le voir
DataGridView1.Columns(4).DisplayIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortByIdentifiant.Click
employes2.Sort()
DataGridView1.DataSource = employes2
DataGridView1.Refresh()
End Sub
End Class |
l'implementation utilise simplement la fonction "shared" String.Comparer.
bon code..............