Ordonner variable tableau : type de données personnalisé

cibleo

XLDnaute Impliqué
Bonsoir le forum,

Il y a peu, j'ai découvert les variables de types de données personnalisées
En reprenant un exemple de J-boisgontier, j'aimerais ordonner des éléments d'une variable tableau selon un ordre établi dans un array.

Voir en Feuil1 les explications, ce sera plus clair.
VB:
Type Personne
  Nom As String
  Note As Integer
  Etape As String
End Type
Sub essai()
  Dim Tablo, Etapes()
  Dim a(1 To 6) As Personne
  Dim temp As Personne
  Etapes = Array(, "Strasbourg", "Mulhouse", "Besançon", "Dijon", "Lyon", "Marseille", "Nice")
  a(1).Nom = "Dupont": a(1).Note = 40: a(1).Etape = "Strasbourg"
  a(2).Nom = "Balu": a(2).Note = 30: a(2).Etape = "Lyon"
  a(3).Nom = "Charlie": a(3).Note = 20: a(3).Etape = "Besançon"
  a(4).Nom = "Durand": a(4).Note = 25: a(4).Etape = "Nice"
  a(5).Nom = "Campas": a(5).Note = 35: a(5).Etape = "Lyon"
  a(6).Nom = "Vidal": a(6).Note = 25: a(6).Etape = "Mulhouse"
'---- tri bubble
  For i = 1 To 6
    For j = i To 6
      If a(j).Note < a(i).Note Then
         temp = a(j)
         a(j) = a(i)
         a(i) = temp
      End If
    Next j
  Next i
  '--
  ReDim Tablo(1 To UBound(a))
  For n = 1 To UBound(a)
    Tablo(n) = a(n).Nom & Chr(10) & a(n).Note & Chr(10) & "-----" & Chr(10) & a(n).Etape
  Next n
  'renvoi les données
  Feuil1.Cells(3, 2).Resize(, UBound(a)) = Application.Transpose(Application.Transpose(Tablo))
End Sub
En fait, il faut remplacer l'algorithme de tri à bulles du code ci-dessus.

Merci de vous pencher sur cet exercice.
Cibleo
 

Pièces jointes

  • Ordonner.xls
    35 KB · Affichages: 32
  • Ordonner.xls
    35 KB · Affichages: 39
  • Ordonner.xls
    35 KB · Affichages: 38

mapomme

XLDnaute Barbatruc
Supporter XLD
Re : Ordonner variable tableau : type de données personnalisé

Bonsoir cibleo,

Essayez ce code qui utilise un objet Scripting.Dictionary.

VB:
Type Personne
  Nom As String
  Note As Integer
  Etape As String
End Type

Sub essai()
  Dim Tablo, Etapes(), i As Long, j As Long, n As Long
  Dim a(1 To 6) As Personne, temp As Personne, Dico
  
  Etapes = Array("Strasbourg", "Mulhouse", "Besançon", "Dijon", "Lyon", "Marseille", "Nice")
  Set Dico = CreateObject("Scripting.Dictionary")
  For i =LBound(Etapes)To UBound(Etapes): Dico.Add Etapes(i), i: Next i
  
  a(1).Nom = "Dupont": a(1).Note = 40: a(1).Etape = "Strasbourg"
  a(2).Nom = "Balu": a(2).Note = 30: a(2).Etape = "Lyon"
  a(3).Nom = "Charlie": a(3).Note = 20: a(3).Etape = "Besançon"
  a(4).Nom = "Durand": a(4).Note = 25: a(4).Etape = "Nice"
  a(5).Nom = "Campas": a(5).Note = 35: a(5).Etape = "Lyon"
  a(6).Nom = "Vidal": a(6).Note = 25: a(6).Etape = "Mulhouse"
'---- tri bubble
  For i = 1 To 6
    For j = i To 6
      If Dico.Item(a(j).Etape) < Dico.Item(a(i).Etape) Then
         temp = a(j)
         a(j) = a(i)
         a(i) = temp
      End If
    Next j
  Next i
  '--
  ReDim Tablo(1 To UBound(a))
  For n = 1 To UBound(a)
    Tablo(n) = a(n).Nom & Chr(10) & a(n).Note & Chr(10) & "-----" & Chr(10) & a(n).Etape
  Next n
  'renvoi les données
  Feuil1.Cells(3, 2).Resize(, UBound(a)) = Application.Transpose(Application.Transpose(Tablo))
End Sub
 

Pièces jointes

  • Ordonner v2.xls
    51 KB · Affichages: 40
Dernière édition:

cibleo

XLDnaute Impliqué
Re : Ordonner variable tableau : type de données personnalisé

Bonjour mapomme :)

Pour bien comprendre, j'ai fait du pas à pas, mais bon les algorithmes de tri ça me donne mal à la tête :p
La variable tableau est bien triée comme je le souhaitais, c'est super.
Je mets ça au chaud, pour l'intégrer dans un futur projet.

Au plaisir Cibleo
 

Discussions similaires

Réponses
11
Affichages
396
Réponses
2
Affichages
370

Statistiques des forums

Discussions
312 837
Messages
2 092 658
Membres
105 482
dernier inscrit
Eric.FKF