I was having the problem with sorting items that had numeric components -
you know the issue where where 11 comes before 2 when you alpha sort
strings. For example
File 1
File 10
File 11
File 2
File 3
etc...
I was using the StrComp function in a procedure similar to yours. I replaced
StrComp with a function that breaks the two strings down into numeric and
non-numeric sections then does the appropriate comparisons. It has the same
return values as StrComp.
There is probably an easier way, but it works for any combination of numeric
and non-numeric characters.
Function AlphaNumStrComp(sComp As String, sBase As String) As Integer
' array of the sBase string seperated into non-numeric and numeric
characters
' Note: the array alternates non-numeric, numeric
Dim baseArray() As String
ReDim baseArray(1 To 1)
' array of the sBase string seperated into non-numeric and numeric
characters
Dim compArray() As String
ReDim compArray(1 To 1)
Dim numflag As Boolean
Dim arPos As Integer
Dim compCnt As Integer
Dim compStr As String
Dim compResult As Integer
Dim I As Integer
' separate the numeric values from the base string
numflag = False
arPos = 1
For I = 1 To Len(sBase)
compStr = Mid(sBase, I, 1)
If IsNumeric(compStr) Then
If numflag = False Then
arPos = arPos + 1
ReDim Preserve baseArray(1 To arPos)
baseArray(arPos) = compStr
numflag = True
Else
baseArray(arPos) = baseArray(arPos) & compStr
End If
Else
If numflag = True Then
arPos = arPos + 1
ReDim Preserve baseArray(1 To arPos)
baseArray(arPos) = compStr
numflag = False
Else
baseArray(arPos) = baseArray(arPos) & compStr
End If
End If
Next
compCnt = arPos
' separate the numeric values from the compare string
numflag = False
arPos = 1
For I = 1 To Len(sComp)
compStr = Mid(sComp, I, 1)
If IsNumeric(compStr) Then
If numflag = False Then
arPos = arPos + 1
ReDim Preserve compArray(1 To arPos)
compArray(arPos) = compStr
numflag = True
Else
compArray(arPos) = compArray(arPos) & compStr
End If
Else
If numflag = True Then
arPos = arPos + 1
ReDim Preserve compArray(1 To arPos)
compArray(arPos) = compStr
numflag = False
Else
compArray(arPos) = compArray(arPos) & compStr
End If
End If
Next
If arPos < compCnt Then
compCnt = arPos
End If
'compare results. If the values are equal, it will progress to the next
array pos
numflag = False
For I = 1 To compCnt
If numflag = False Then
' do an alpha compare
compResult = StrComp(compArray(I), baseArray(I), vbTextCompare)
If compResult = -1 Then
GoTo ValLessThan
ElseIf compResult = 1 Then
GoTo ValGreaterThan
End If
' values are the same, proceed to the next array position
numflag = True
Else
' do a numeric compare
If CDbl(compArray(I)) < CDbl(baseArray(I)) Then
GoTo ValLessThan
ElseIf CDbl(compArray(I)) > CDbl(baseArray(I)) Then
GoTo ValGreaterThan
End If
' values are the same, proceed to the next array position
numflag = False
End If
Next
' the values are identical
AlphaNumStrComp = 0
Exit Function
' the compare value is less then the base value
ValLessThan:
AlphaNumStrComp = -1
Exit Function
' the compare value is less then the base value
ValGreaterThan:
AlphaNumStrComp = 1
End Function