### Sort and unique any dynamic array

The code example below resorts and filters out duplicate entries in any dynamic array. The array you pass to it must be dynamic, not fixed.
LotusScript

```Sub Initialize

Dim aa() As String
Redim aa(5) As String

aa(0) = "Frank"
aa(1) = "Ed"
aa(2) = "Shawn"
aa(3) = "Frank"
aa(4) = "Frank"
aa(5) = "JoAnne"

Call SortAndUniqueArray(aa)

End Sub```

```Sub SortAndUniqueArray(array)

'*********************************************************************************************
'Resort array in ascending order
n = Ubound(Array) + 1

For X = 1 To (n - 1)                                     'Perform the following loop for each value in the arrays
J = X
Do While J >= 1
If Array(J) < Array(J - 1) Then     ' Compares two values in the array
ValueA = Array(J)                ' Swap the values since the second is less than the first
ValueB = Array(J - 1)
Array(J) = ValueB
Array(J - 1) = ValueA
J = J - 1                               ' Go to the next two down in the array (descending to index 0 )
Else
Exit Do                                ' Index 0 reached, goto next X index in the array and loop again
End If
Loop
Next

'*********************************************************************************************
'Build new temp array with unique values
Dim k As Long
Dim UniqueArray() As String
Redim UniqueArray(k) As String

For p = 0 To Ubound(array)
If p = 0 Then
UniqueArray(k) = array(p)
Else
If Not Array(p) = Array(p-1) Then
k = k + 1
Redim Preserve UniqueArray(k) As String
UniqueArray(k) = array(p)
End If
End If
Next

'*********************************************************************************************
'Migrate values back into original array
Redim Array(Ubound(UniqueArray)) As String
For t = 0 To Ubound(UniqueArray)
Array(t) = UniqueArray(t)
Next
'*********************************************************************************************

End Sub```

```ORIGINAL ARRAY:
aa(0) = "Frank"
aa(1) = "Ed"
aa(2) = "Shawn"
aa(3) = "Frank"
aa(4) = "Frank"
aa(5) = "JoAnne"

RETURN ARRAY:
aa(0) = "Ed"
aa(1) = "Frank"
aa(2) = "JoAnne"
aa(3) = "Shawn"```

Posted by fbrefere001 on Thursday January 23, 2003