开发者

WORD VBA Sort Ascending and Sort Descending

开发者 https://www.devze.com 2023-03-01 18:40 出处:网络
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. Pl

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

精彩评论

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