Autor Tema: [Question] How do I get done sorting the size of  (Leído 2195 veces)

0 Usuarios y 1 Visitante están viendo este tema.

polosa

  • Bytes
  • *
  • Mensajes: 14
  • Reputación: +0/-0
    • Ver Perfil
[Question] How do I get done sorting the size of
« en: Octubre 31, 2010, 10:51:20 am »
Hi All

This code can be done by the sort to a-z. Would like to ask how to modify the sort done by the to z-a.

Please help me

Código: [Seleccionar]

 Option Explicit


 Private Sub Form_Load()

     Dim MyStrArray() As String, K As Long, Q As Long

     ReDim MyStrArray(1 To 10)



     Randomize



     Debug.Print "Unsorted strings:"

     For K = LBound(MyStrArray) To UBound(MyStrArray)



         ' create a random string

         MyStrArray(K) = String(10, " ")

         For Q = 1 To 10

             Mid$(MyStrArray(K), Q, 1) = Chr(Asc("A") + Fix(26 * Rnd))

         Next Q



         ' print the string to the immediate window

         Debug.Print MyStrArray(K)

     Next K



     ' sort the array

     QuickSort MyStrArray, LBound(MyStrArray), UBound(MyStrArray)



     ' print the sorted string to the immediate window

     Debug.Print vbNewLine & "Sorted strings:"

     For K = LBound(MyStrArray) To UBound(MyStrArray)

         Debug.Print MyStrArray(K)

     Next K

 End Sub

 Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long)
     Dim low As Long, high As Long
     Dim MidValue As String

     low = First
     high = Last
     MidValue = C((First + Last) \ 2)

     Do

         While C(low) < MidValue
             low = low + 1
         Wend

         While C(high) > MidValue
             high = high - 1
         Wend

         If low <= high Then
             Swap C(low), C(high)

             low = low + 1

             high = high - 1
         End If
     Loop While low <= high

     If First < high Then QuickSort C, First, high
     If low < Last Then QuickSort C, low, Last
 End Sub
 Private Sub Swap(ByRef A As String, ByRef B As String)
     Dim T As String
     T = A
     A = B
     B = T
 End Sub

polosa

  • Bytes
  • *
  • Mensajes: 14
  • Reputación: +0/-0
    • Ver Perfil
Re:[Question] How do I get done sorting the size of
« Respuesta #1 en: Octubre 31, 2010, 08:25:11 pm »
i test ok
Código: [Seleccionar]
Private Sub MyQuickSort_Single(ByRef SortArray As Variant, ByVal First As Long, ByVal Last As Long, _
                                                        ByVal PrimeSort As Integer, ByVal Ascending as Boolean)
Dim Low As Long, High As Long
Dim Temp As Variant, List_Separator As Variant
Dim TempArray() As Variant
ReDim TempArray(UBound(SortArray, 1))
Low = First
High = Last
List_Separator1 = SortArray(PrimeSort, (First + Last) / 2)
Do
    If Ascending = True Then
        Do While (SortArray(PrimeSort, Low) < List_Separator1)
            Low = Low + 1
        Loop
        Do While (SortArray(PrimeSort, High) > List_Separator1)
            High = High - 1
        Loop
    Else
        Do While (SortArray(PrimeSort, Low) > List_Separator1)
            Low = Low + 1
        Loop
        Do While (SortArray(PrimeSort, High) < List_Separator1)
            High = High - 1
        Loop
    End If
    If (Low <= High) Then
        For i = LBound(SortArray, 1) to UBound(SortArray, 1)
            TempArray(i) = SortArray(i, Low)
        Next
        For i = LBound(SortArray, 1) to UBound(SortArray, 1)
            SortArray(i, Low) = SortArray(i, High)
        Next
        For i = LBound(SortArray, 1) to UBound(SortArray, 1)
            SortArray(i, High) = TempArray(i)
        Next
        Low = Low + 1
        High = High - 1
    End If
Loop While (Low <= High)
If (First < High) Then MyQuickSort_Single SortArray, First, High, PrimeSort, Ascending
If (Low < Last) Then MyQuickSort_Single SortArray, Low, Last, PrimeSort, Ascending
End Sub

Psyke1

  • Megabyte
  • ***
  • Mensajes: 130
  • Reputación: +11/-7
  • VBManiac
    • Ver Perfil
    • h-Sec