VBScript strnatcasecmp function

A VBScript equivalent of PHP’s strnatcasecmp


Function strnatcasecmp( str1, str2 )

    Dim array1,array2
    array1 = strnatcmp_helper(str1)
    array2 = strnatcmp_helper(str2)

    Dim intlen,text,result,r
    intlen = uBound(array1)
    text   = true

    result = -1
    r      = 0

    if intlen > uBound(array2) Then
        intlen = uBound(array2)
        result = 1
    End If

    Dim i
    strnatcasecmp = false
    For i = 0 to intlen
        If not isNumeric( array1(i) ) Then
            If Not isNumeric( array2(i) ) Then
                text = true

                r = strcasecmp(array1(i),array2(i))
                If r <> 0 Then
                    strnatcasecmp = r
                End If

            ElseIf text Then
                strnatcasecmp = 1
            Else
                strnatcasecmp = 1
            End If

        ElseIf not isNumeric( array2(i) ) Then
            If text Then
                strnatcasecmp = -1
            Else
                strnatcasecmp = 1
            End If
        Else
            If text Then
                r = array1(i) - array2(i)
                If r <> 0 Then
                    strnatcasecmp = r
                End If
            Else
                r = strcasecmp(array1(i),array2(i))
                If r <> 0 Then
                    strnatcasecmp = r
                End If
            End If

            text = false
        End If

        if [!==](strnatcasecmp,false) Then Exit Function
    Next

    strnatcasecmp = result

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the strings extension