Hi Guys
Can Anybody help me with this, i need to sort a string
let say i have a string like: "this is a test 1"
And i want to sort it like "1aehiissstt"
thanks
Printable View
Hi Guys
Can Anybody help me with this, i need to sort a string
let say i have a string like: "this is a test 1"
And i want to sort it like "1aehiissstt"
thanks
Maybe something like....
Code:Private Sub Command1_Click()
Dim sMyTxt As String
' text
sMyTxt = "this is a test 1"
' sort to single line
sMyTxt = SortToSingleLine(sMyTxt)
' show result in immediate window
Debug.Print sMyTxt
End Sub
Private Function SortToSingleLine(sTxt As String) As String
Dim l As Long
' remove single spaces
sTxt = Replace(sTxt, " ", "")
' make/size string array
ReDim sArray(Len(sTxt)) As String
' copy txt to array
For l = 1 To Len(sTxt)
sArray(l - 1) = Mid$(sTxt, l, 1)
Next l
' erase original text
sTxt = vbNullString
' sort array
QuickSort sArray()
' copy sorted array to single string
For l = 0 To UBound(sArray)
sTxt = sTxt & sArray(l)
Next l
' return results
SortToSingleLine = sTxt
End Function
Private Sub QuickSort(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 QuickSort pvarArray, plngLeft, lngLast
If lngFirst < plngRight Then QuickSort pvarArray, lngFirst, plngRight
End Sub
thank you very very much
Ciao ragazzi sono nuovo non riesco a trovare una discussione dove possa capire come ordinare il mio file dove all'interno ci sono registrati dei nomi di persona. il mio problema consiste che sono alle prime armi con l'utilizzo del linguaggio visual basic 2005 e vorrei avere il vostro aiuto.
la mia richiesta di aiuto consiste:
ho un file dove vado a registrare i dati anagrafici di determinate persone, i dati giustamente vengono inseriti non in ordine alfabetico, ora io quando li vado a visualizzare li vorrei visualizzare in ordine alfabetico, ho visto la funzione quicksort ma non riesco a comprenderla potreste aiutarmi perfavore in questo mio problema e anche nel secondo cioè dove spostare questa discussione per non intralciare questa? Grazie a tutti quelli che mi aiuteranno ( perfavore non attaccatemi per la mia incompetenza :p )
Roughly translated:
Hello guys I'm new I can not find a discussion where I can figure out how to order my files inside where there are recorded the names of the person. my problem is that they are just starting out with the use of language Visual Basic 2005, and I would like your help.
my request for aid consists of:
I have a file where I go to record the personal data of certain persons, the data are entered correctly not in alphabetical order, now I'm going to see them when I see them in alphabetical order, I saw the quicksort function but I can not understand could you help me please let this my problem and that is where in the second move this discussion does not hinder this? Thanks to all who help me.
mcoulter876 have you translate my post? why?