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
|
Option Compare Text
Type Personne
Nom As String
Dte As Long
End Type
Sub triMultiCriteres()
n = 5
Dim a() As Personne: ReDim a(1 To n)
a(1).Nom = "Dupont": a(1).Dte = #1/1/1980#
a(2).Nom = "Balu": a(2).Dte = #1/1/1980#
a(3).Nom = "Charlie": a(3).Dte = #1/1/1990#
a(4).Nom = "Durand": a(4).Dte = #1/1/1990#
a(5).Nom = "Campas": a(5).Dte = #1/1/1980#
'--- tri multi-criteres Date/nom
Dim b() As Personne: ReDim b(LBound(a) To UBound(a))
Dim clé() As String: ReDim clé(LBound(a) To UBound(a))
Dim index() As Long: ReDim index(LBound(a) To UBound(a, 1))
For i = LBound(a) To UBound(a, 1)
clé(i) = Format(a(i).Dte, "000000") & a(i).Nom: index(i) = i
Next i
Call Tri(clé(), index(), LBound(a), UBound(clé))
For lig = LBound(clé) To UBound(clé)
b(lig) = a(index(lig))
Next lig
a = b
'-- transfert feuille pour test
For i = 1 To n
Cells(i + 1, 1) = a(i).Nom
Cells(i + 1, 2) = a(i).Dte
Next i
End Sub
Sub Tri(clé() As String, index() As Long, gauc, droi) ' Quick sort
ref = clé((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While clé(g) < ref: g = g + 1: Loop
Do While ref < clé(d): d = d - 1: Loop
If g <= d Then
temp = clé(g): clé(g) = clé(d): clé(d) = temp
temp = index(g): index(g) = index(d): index(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call Tri(clé, index, g, droi)
If gauc < d Then Call Tri(clé, index, gauc, d)
End Sub |
Partager