Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: polosa 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
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
-
i test ok
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
-
Look :
http://www.leandroascierto.com.ar/foro/index.php?topic=453.0
DoEvents! :P