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
|
Public Sub TrierTbl()
' Remplissage du tableau
Dim Tbl(1 To 6, 1 To 4)
For i = 1 To UBound(Tbl, 1)
Tbl(i, 1) = "Client " & UBound(Tbl, 1) - i
Tbl(i, 2) = "Adresse " & UBound(Tbl, 1) - i
Tbl(i, 3) = "Code postal " & UBound(Tbl, 1) - i
Tbl(i, 4) = "Ville " & UBound(Tbl, 1) - i
Next i
Debug.Print Tbl(1, 1)
TriTableauIndex Tbl()
Debug.Print Tbl(1, 1)
End Sub
Sub TriTableauIndex(b)
Dim Tmp(): ReDim Tmp(LBound(b) To UBound(b), LBound(b, 2) To UBound(b, 2))
Dim clé() As String: ReDim clé(LBound(b) To UBound(b))
Dim index() As Long: ReDim index(LBound(b) To UBound(b, 1))
For i = LBound(b) To UBound(b)
clé(i) = b(i, 1): index(i) = i
Next i
TriIndex clé(), index(), LBound(b), UBound(clé)
For lig = LBound(clé) To UBound(clé)
For col = LBound(b, 2) To UBound(b, 2): Tmp(lig, col) = b(index(lig), col): Next col
Next lig
b = Tmp
End Sub
Sub TriIndex(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 TriIndex clé, index, g, droi
If gauc < d Then TriIndex clé, index, gauc, d
End Sub |
Partager