IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

VB.NET, list(of T) et sort


Sujet :

VB.NET

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 21
    Par défaut VB.NET, list(of T) et sort
    Bonsoir,

    Nouvelle problématique:

    J'ai une classe "personne" qui inclut les éléments suivants:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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..............

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 21
    Par défaut
    Merci, ça marche nickel

  4. #4
    Expert éminent Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 200
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 200
    Par défaut
    ou (from p as personne in lacollection select p order by p.identifiant)
    éventuellement .tolist
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [VB.NET] List(Of et la function Find
    Par ricil78 dans le forum Windows Forms
    Réponses: 5
    Dernier message: 05/01/2007, 15h29
  2. [Access & ADO.Net] Liste de choix
    Par oodini dans le forum Access
    Réponses: 12
    Dernier message: 24/06/2006, 17h57
  3. list d'objet et sort()
    Par mikebranque dans le forum C++
    Réponses: 6
    Dernier message: 18/03/2006, 19h05
  4. [VB.NET] Listing d'un répertoire par FTP avec API
    Par joefou dans le forum VB.NET
    Réponses: 1
    Dernier message: 16/12/2005, 15h29
  5. [VB.NET] Liste composant
    Par borgfabr dans le forum Windows Forms
    Réponses: 4
    Dernier message: 12/04/2005, 18h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo