Sub IndexerFus1Col(TIdx() As Long, TDon(), _
Optional ByVal Croissant As Boolean = True, Optional ByVal LMax As Long)
Dim NR As Long, ÀFusionner As New Collection, _
L1 As Long, TFus1() As Long, N1 As Long, Arg1, _
L2 As Long, TFus2() As Long, N2 As Long, Arg2
If LMax <= 0 Then LMax = UBound(TDon, 1)
ReDim TIdx(1 To &HFFF&)
NR = 1: TIdx(1) = 1: L1 = 1: Arg1 = TDon(1, 1)
If VarType(Arg1) = vbString Then If Arg1 = "" Then Arg1 = Empty: TDon(1, 1) = Empty
For L2 = 2 To LMax: Arg2 = TDon(L2, 1)
If VarType(Arg2) = vbString Then If Arg2 = "" Then Arg2 = Empty: TDon(L2, 1) = Empty
If DansLOrdre(Arg1, Arg2, Croissant, L1 < L2) Then
NR = NR + 1: If NR > UBound(TIdx) Then ReDim Preserve TIdx(1 To NR Or &HFFF&)
TIdx(NR) = L2: L1 = L2: Arg1 = Arg2
ElseIf NR < 20 Then
For N1 = 1 To NR - 1: L1 = TIdx(N1): If DansLOrdre(Arg2, TDon(L1, 1), Croissant, L1 > L2) Then Exit For
Next N1
NR = NR + 1: If NR > UBound(TIdx) Then ReDim Preserve TIdx(1 To NR Or &HFFF&)
For N2 = NR To N1 + 1 Step -1: TIdx(N2) = TIdx(N2 - 1): Next N2
TIdx(N1) = L2: L1 = TIdx(NR): Arg1 = TDon(L1, 1)
Else: ReDim Preserve TIdx(1 To NR): ÀFusionner.Add TIdx: NR = 1
TIdx(1) = L2: L1 = L2: Arg1 = Arg2: End If
Next L2
ReDim Preserve TIdx(1 To NR)
Do While ÀFusionner.Count > 0
ÀFusionner.Add TIdx
TFus1 = ÀFusionner(1): N1 = 1: ÀFusionner.Remove 1
TFus2 = ÀFusionner(1): N2 = 1: ÀFusionner.Remove 1
ReDim TIdx(1 To UBound(TFus1) + UBound(TFus2))
L1 = TFus1(1): Arg1 = TDon(L1, 1)
L2 = TFus2(1): Arg2 = TDon(L2, 1)
NR = 0: Do: NR = NR + 1
If DansLOrdre(Arg1, Arg2, Croissant, L1 < L2) Then
TIdx(NR) = L1: N1 = N1 + 1: If N1 <= UBound(TFus1) Then L1 = TFus1(N1): Arg1 = TDon(L1, 1) Else GoTo Fin2
Else: TIdx(NR) = L2: N2 = N2 + 1: If N2 <= UBound(TFus2) Then L2 = TFus2(N2): Arg2 = TDon(L2, 1) Else Exit Do
End If
Loop
Do: NR = NR + 1: TIdx(NR) = TFus1(N1): N1 = N1 + 1: Loop Until N1 > UBound(TFus1): GoTo FusS
Fin2: Do: NR = NR + 1: TIdx(NR) = TFus2(N2): N2 = N2 + 1: Loop Until N2 > UBound(TFus2)
FusS: Loop
End Sub
Private Function DansLOrdre(ByVal Val1, ByVal Val2, ByVal Croissant As Boolean, ByVal ParDéf As Boolean) As Boolean
Dim Typ1 As VbVarType, Typ2 As VbVarType, Sens As Integer, Comp As Integer
Sens = 2 * -Croissant - 1
Typ1 = VarType(Val1): Typ2 = VarType(Val2)
Select Case Typ2
Case Is <> Typ1: Comp = Sgn(Typ2 - Typ1)
Case vbString: Comp = StrComp(Val2, Val1) * Sens
Case vbError: Comp = Sgn(CLng(Val2) - CLng(Val1)) * Sens
Case vbEmpty: Comp = 0
Case Else: Comp = Sgn(Val2 - Val1) * Sens: End Select
If Comp = 0 Then DansLOrdre = ParDéf Else DansLOrdre = Comp > 0
End Function