Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: polosa en Septiembre 11, 2011, 11:44:47 am
-
My English is bad, please forgive me
I use the following code I have an array of strings arranged in Malay
But found a problem. That is, the number of strings arranged in front of the wrong. As follows:
1.abcdefg
10.abcdefg
11.abcdefg
12.abcdefg
13.abcdefg
14.abcdefg
15.abcdefg
16.abcdefg
2.abcdefg
3.abcdefg
4.abcdefg
5.abcdefg
6.abcdefg
7.abcdefg
8.abcdefg
9.abcdefg
How can I use a string array can be sorted into the following
1.abcdefg
2.abcdefg
3.abcdefg
4.abcdefg
5.abcdefg
6.abcdefg
7.abcdefg
8.abcdefg
9.abcdefg
10.abcdefg
11.abcdefg
12.abcdefg
13.abcdefg
14.abcdefg
15.abcdefg
16.abcdefg
thank
Private Sub SortStrings(ByRef sArray() As String, ByVal nFirst As Integer, ByVal nLast As Integer)
Dim nBoundary As Integer
Dim i As Integer
If nLast <= nFirst Then Exit Sub
SwapS sArray(nFirst), sArray((nFirst + nLast) / 2)
nBoundary = nFirst
For i = nFirst + 1 To nLast
If StrComp(sArray(nFirst), sArray(i), vbTextCompare) = 1 Then
nBoundary = nBoundary + 1
SwapS sArray(nBoundary), sArray(i)
End If
Next
SwapS sArray(nFirst), sArray(nBoundary)
SortStrings sArray(), nFirst, nBoundary - 1
SortStrings sArray(), nBoundary + 1, nLast
End Sub
Private Sub SwapS(ByRef Str1 As String, ByRef Str2 As String)
Dim sTemp As String
sTemp = Str1
Str1 = Str2
Str2 = sTemp
End Sub
-
Its because you're using StrCmp, and you have to get the first number (do a Mid$ + InStr to get the substring) and then compare it.
something like this
nPos = InStr(str, ".")
if nPos > 0 then iLineNumberA = Mid$(str, 1, nPos)
If iLineNumberA > iLineNumberB Then
' ...
-
That is because you are using an alphabetical sorting algorithm instead of a numerical.
-
I have thought about. Just does not string value in the bear know how to deal with, please advise.
-
always have this fromat xxxxx . yyyyyyyy
xxxxx = number dot yyyyyy=string
?
-
try this:
Sort Module For Strings and Numbers (http://www.freevbcode.com/ShowCode.asp?ID=2189)
-
How can I use a string array can be sorted into the following
Hi, if you always have the string in this format ( number dot string ) you can do in the way that raul338 said
i code this a variant using split function:
full code here http://www.megaupload.com/?d=MMK0XBAE (http://www.megaupload.com/?d=MMK0XBAE)
i wrote this function that receives one string then splits, the dot is used like delimiter and returns left part of dot
Example
string format espected number dot string , return number of string
string "11.abcdefg" split into array ("11" "abcdefg") return val("11")
Private Function values(cadena As String) As Double
Dim aux() As String
aux = Split(cadena, ".", , vbTextCompare)
values = Val(aux(0))
End Function
and then you only have to substitute the strcmp function into values function
Private Sub SortStrings(ByRef sArray() As String, ByVal nFirst As Integer, ByVal nLast As Integer)
Dim nBoundary As Integer
Dim i As Integer
If nLast <= nFirst Then Exit Sub
SwapS sArray(nFirst), sArray((nFirst + nLast) / 2)
nBoundary = nFirst
For i = nFirst + 1 To nLast
If values(sArray(nFirst)) > values(sArray(i)) Then
nBoundary = nBoundary + 1
SwapS sArray(nBoundary), sArray(i)
End If
Next
SwapS sArray(nFirst), sArray(nBoundary)
SortStrings sArray(), nFirst, nBoundary - 1
SortStrings sArray(), nBoundary + 1, nLast
End Sub
Private Sub SwapS(ByRef Str1 As String, ByRef Str2 As String)
Dim sTemp As String
sTemp = Str1
Str1 = Str2
Str2 = sTemp
End Sub
I hope this was helpful to you
Regards