Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

tri collection

2 réponses
Avatar
Jacques
Bonjour,

J'ai créé une collection et je voudrais que celle-ci soit trié par ordre
alphabétique.
exemple :

Tabl = Array("xxxx", "gggg", "aaaa", "jjjj", "kkkk")
Set Tabl2 = CreateObject("Scripting.Dictionary")
i = 0
j = 0
For Each Item In Tabl
Tabl2.Add j, Tabl(i)
i = i + 1
j = j + 1
Next Item

'trier la collecion Tabl2


Comment faire ?

Jacques

2 réponses

Avatar
Jacques a écrit :

Bonjour,

J'ai créé une collection et je voudrais que celle-ci soit trié par ordre
alphabétique.
exemple :

Tabl = Array("xxxx", "gggg", "aaaa", "jjjj", "kkkk")
Set Tabl2 = CreateObject("Scripting.Dictionary")
i = 0
j = 0
For Each Item In Tabl
Tabl2.Add j, Tabl(i)
i = i + 1
j = j + 1
Next Item

'trier la collecion Tabl2


Comment faire ?

Jacques






Bonjour,

Une question , Tabl c'est un tableau non ?, pas une collection ?

Christophe
Avatar
Guy DETIENNE
Salut ;O)


A coller dans un module :

Utilisation

Dim MyCollection As New Collection
Set MyCollection = SortCollection(MyCollection)

Guy


Public Function SortCollection(ByVal C As Collection) As Collection

Dim n As Long: n = C.Count

If n = 0 Then Set SortCollection = New Collection: Exit Function

ReDim Index(0 To n - 1) As Long ' allocate index array

Dim i As Long, m As Long

For i = 0 To n - 1: Index(i) = i + 1: Next ' fill index array
For i = n 2 - 1 To 0 Step -1 ' generate ordered
heap
Heapify C, Index, i, n
Next
For m = n To 2 Step -1 ' sort the index array
Exchange Index, 0, m - 1 ' move highest element
to top
Heapify C, Index, 0, m - 1
Next
Dim c2 As New Collection
For i = 0 To n - 1: c2.add C.Item(Index(i)): Next ' fill output
collection
Set SortCollection = c2

End Function

Private Sub Heapify(ByVal C As Collection, Index() As Long, ByVal i1 As
Long, ByVal n As Long)
' Heap order rule: a[i] >= a[2*i+1] and a[i] >= a[2*i+2]
Dim nDiv2 As Long: nDiv2 = n 2
Dim i As Long: i = i1
Do While i < nDiv2
Dim K As Long: K = 2 * i + 1
If K + 1 < n Then
If C.Item(Index(K)) < C.Item(Index(K + 1)) Then K = K + 1
End If
If C.Item(Index(i)) >= C.Item(Index(K)) Then Exit Do
Exchange Index, i, K
i = K
Loop
End Sub

Private Sub Exchange(Index() As Long, ByVal i As Long, ByVal j As Long)
Dim Temp As Long: Temp = Index(i)
Index(i) = Index(j)
Index(j) = Temp
End Sub
"Jacques" a écrit dans le message de
news:432fa38d$0$1747$
Bonjour,

J'ai créé une collection et je voudrais que celle-ci soit trié par ordre
alphabétique.
exemple :

Tabl = Array("xxxx", "gggg", "aaaa", "jjjj", "kkkk")
Set Tabl2 = CreateObject("Scripting.Dictionary")
i = 0
j = 0
For Each Item In Tabl
Tabl2.Add j, Tabl(i)
i = i + 1
j = j + 1
Next Item

'trier la collecion Tabl2


Comment faire ?

Jacques