开发者

WORD VBA Sort Ascending and Sort Descending

Here is code I have that sorts the words inside an array into ascending order. I need help to change it in such a way that it will also sort the words in descending order, all in a single function. Please开发者_运维百科 help. Thanks!

Function Sort_Asc(ByRef str() As String)
     Dim iLower As Integer, iUpper As Integer, iCount As Integer, Temp As String
     Dim str2 As String
           iUpper = UBound(str)
           iLower = 1
           Dim bSorted As Boolean
           bSorted = False
           Do While Not bSorted
                bSorted = True
                For iCount = iLower To iUpper - 1
                str2 = StrComp(str(iCount), str(iCount + 1), vbTextCompare)
                     If str2 = 1 Then
                           Temp = str(iCount + 1)
                           str(iCount + 1) = str(iCount)
                           str(iCount) = Temp
                           bSorted = False
                     End If
                Next iCount
             iUpper = iUpper - 1
           Loop
End Function


How about

Function Sort(ByRef str() As String, ByVal booAsc As Boolean)
 Dim iLower As Integer, iUpper As Integer, iCount As Integer, Temp As String
 Dim str2 As String

       iUpper = UBound(str)
       iLower = 1

       Dim bSorted As Boolean
       bSorted = False
       Do While Not bSorted
            bSorted = True
            For iCount = iLower To iUpper - 1
            If booAsc Then
                str2 = StrComp(str(iCount + 1), str(iCount), vbTextCompare)
            Else
                str2 = StrComp(str(iCount), str(iCount + 1), vbTextCompare)
            End If
                 If str2 = 1 Then
                       Temp = str(iCount + 1)
                       str(iCount + 1) = str(iCount)
                       str(iCount) = Temp
                       bSorted = False
                 End If
            Next iCount
         iUpper = iUpper - 1
       Loop
End Function

and call the function using Sort strArray, False '(False Ascending, True Descending)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜