hi all,
just want an opinion from all of you out there on how to do descending sort. I got this sort example code from the net but it does ascending sort. Is the a possibility that if I change a few line inside the code it will come out as descending sort? I have a text file which the output will be like this:
0:1
2:3
3.5:2
2.3:7
5:4
and i want to sort it descending based on the first word before the (:) sign.
thanks..

Public Sub SortTextFile(pstrFile As String)
   Dim strLine() As String
 
   strLine = Split(LoadFileToString(pstrFile), vbNewLine)
   QuickSort1 strLine
   SaveStringToFile Join(strLine, vbNewLine), pstrFile
End Sub
 
Public Function LoadFileToString(pstrFile As String) As String
   Dim bytArray() As Byte
   Dim strReturn As String
   Dim FileNumber As Long
 
   FileNumber = FreeFile
   Open pstrFile For Binary Access Read As #FileNumber
   ReDim bytArray(LOF(FileNumber) - 1)
   Get #FileNumber, 1, bytArray
   Close #FileNumber
   strReturn = StrConv(bytArray, vbUnicode)
   Erase bytArray
   Do While InStr(strReturn, vbNewLine & vbNewLine)
       strReturn = Replace(strReturn, vbNewLine & vbNewLine, vbNewLine)
   Loop
   Do While Right(strReturn, 2) = vbNewLine
       strReturn = Left(strReturn, Len(strReturn) - 2)
   Loop
   LoadFileToString = strReturn
End Function
 
Public Sub SaveStringToFile(pstrText As String, pstrFile As String)
   Dim FileNumber As Long
 
   If Len(Dir(pstrFile)) Then Kill pstrFile
   FileNumber = FreeFile()
   Open pstrFile For Output As #FileNumber
   Print #FileNumber, pstrText
   Close
End Sub
 
' Omit plngLeft & plngRight; they are used internally during recursion
Public Sub QuickSort1(ByRef pvarArray As Variant, Optional ByVal plngLeft As Long, Optional ByVal plngRight As Long)
   Dim lngFirst As Long
   Dim lngLast As Long
   Dim varMid As Variant
   Dim varSwap As Variant
 
   If plngRight = 0 Then
       plngLeft = LBound(pvarArray)
       plngRight = UBound(pvarArray)
   End If
   lngFirst = plngLeft
   lngLast = plngRight
   varMid = pvarArray((plngLeft + plngRight) \ 2)
   Do
       Do While pvarArray(lngFirst) < varMid And lngFirst < plngRight
           lngFirst = lngFirst + 1
       Loop
       Do While varMid < pvarArray(lngLast) And lngLast > plngLeft
           lngLast = lngLast - 1
       Loop
       If lngFirst <= lngLast Then
           varSwap = pvarArray(lngFirst)
           pvarArray(lngFirst) = pvarArray(lngLast)
           pvarArray(lngLast) = varSwap
           lngFirst = lngFirst + 1
           lngLast = lngLast - 1
       End If
   Loop Until lngFirst > lngLast
   If plngLeft < lngLast Then QuickSort1 pvarArray, plngLeft, lngLast
   If lngFirst < plngRight Then QuickSort1 pvarArray, lngFirst, plngRight
End Sub

Swap the lngFirst and lngLast parts around. That will then display descending.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.