Is An Array Sorted

This page describes procedures you can use to determine whether an array is in sorted order.

Introduction

Sorting is an expensive operation in term is processing overhead and memory accesss. Before you undertake a sorting operation on a large array, you should test whether that array is already sorted. If it is, then you do not have to sort the array. This can save substantial processing overhead with large arrays. This page describes two procedures, IsNumericArraySorted and IsStringArraySorted that return True if the input array is sorted, or False if the input array is not sorted.

IsNumericArraySorted

This function takes as input an array of numeric values and returns True if they are in sorted order or False if they are not in sorted order.

```Function IsNumericArraySorted(Arr As Variant) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsNumericArraySorted
' This examines the array Arr (which must contain all
' numeric values) and returns True if Arr is in sorted
' order or False if it is not in sorted order.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim N As Long
Dim L As Long

If IsArray(Arr) = False Then
IsNumericArraySorted = False
Exit Function
End If
If UBound(Arr) - LBound(Arr) + 1 = 1 Then
' array has one element.
IsNumericArraySorted = True
Exit Function
End If

If Arr(LBound(Arr)) < Arr(LBound(Arr) + 1) Then
L = 1
ElseIf Arr(LBound(Arr)) = Arr(LBound(Arr) + 1) Then
L = 0
ElseIf Arr(LBound(Arr)) > Arr(LBound(Arr) + 1) Then
L = -1
End If

For N = LBound(Arr) To UBound(Arr) - 1
If Arr(N) < Arr(N + 1) Then
If L = 0 Then
L = 1
ElseIf L = 1 Then
' ok
ElseIf L = -1 Then
IsNumericArraySorted = False
Exit Function
End If
ElseIf Arr(N) > Arr(N + 1) Then
If L = 0 Then
L = -1
ElseIf L = -1 Then
' ok
ElseIf L = 1 Then
IsNumericArraySorted = False
Exit Function
End If
End If
Next N
IsNumericArraySorted = True

End Function
```

IsStringArraySorted

This function takes as input an array of string values and returns True if they are in sorted order or False if they are not in sorted order. You can specify whether the comparison take upper and lower case into account. For case sensitive comparisons, set CompareMethod to vbBinaryCompare. For case insensitive comparisons, set CompareMethod to vbTextCompare. The code for IsStringArraySorted is shown below:

```Function IsStringArraySorted(Arr As Variant, CompareMethod As VbCompareMethod) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsStringArraySorted
' This examines the array Arr (which must contain all
' string values) and returns True if Arr is in sorted
' order or False if it is not in sorted order.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim N As Long
Dim L As Long

If IsArray(Arr) = False Then
IsStringArraySorted = False
Exit Function
End If
If UBound(Arr) - LBound(Arr) + 1 = 1 Then
' array has one element.
IsStringArraySorted = True
Exit Function
End If

If StrComp(Arr(LBound(Arr)), Arr(LBound(Arr) + 1), CompareMethod) = -1 Then
L = -1
ElseIf StrComp(Arr(LBound(Arr)), Arr(LBound(Arr) + 1), CompareMethod) = 0 Then
L = 0
ElseIf StrComp(Arr(LBound(Arr)), Arr(LBound(Arr) + 1), CompareMethod) = 1 Then
L = 1
End If

For N = LBound(Arr) To UBound(Arr) - 1
If StrComp(Arr(N), Arr(N + 1), CompareMethod) = 1 Then
If L = 0 Then
L = 1
ElseIf L = 1 Then
' ok
ElseIf L = -1 Then
IsStringArraySorted = False
Exit Function
End If

ElseIf StrComp(Arr(N), Arr(N + 1), CompareMethod) = -1 Then
If L = 0 Then
L = -1
ElseIf L = -1 Then
' ok
ElseIf L = 1 Then
IsStringArraySorted = False
Exit Function
End If
End If
Next N
IsStringArraySorted = True

End Function
```