Autor Tema: [Problem] What about the problem of sorting a string array  (Leído 5133 veces)

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

polosa

  • Bytes
  • *
  • Mensajes: 14
  • Reputación: +0/-0
    • Ver Perfil
[Problem] What about the problem of sorting a string array
« 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

Código: [Seleccionar]
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

raul338

  • Terabyte
  • *****
  • Mensajes: 894
  • Reputación: +62/-8
  • xD fan!!!!! xD
    • Ver Perfil
    • Raul's Weblog
Re:[Problem] What about the problem of sorting a string array
« Respuesta #1 en: Septiembre 11, 2011, 11:51:04 am »
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
Código: (vb) [Seleccionar]
nPos = InStr(str, ".")
if nPos > 0 then iLineNumberA = Mid$(str, 1, nPos)
If iLineNumberA > iLineNumberB Then
 ' ...

cobein

  • Moderador Global
  • Gigabyte
  • *****
  • Mensajes: 348
  • Reputación: +63/-0
  • Más Argentino que el morcipan
    • Ver Perfil
Re:[Problem] What about the problem of sorting a string array
« Respuesta #2 en: Septiembre 11, 2011, 09:34:09 pm »
That is because you are using an alphabetical sorting algorithm instead of a numerical.

polosa

  • Bytes
  • *
  • Mensajes: 14
  • Reputación: +0/-0
    • Ver Perfil
Re:[Problem] What about the problem of sorting a string array
« Respuesta #3 en: Septiembre 11, 2011, 10:54:46 pm »
I have thought about. Just does not string value in the bear know how to deal with, please advise.

czar9

  • Kilobyte
  • **
  • Mensajes: 64
  • Reputación: +4/-4
    • Ver Perfil
Re:[Problem] What about the problem of sorting a string array
« Respuesta #4 en: Septiembre 18, 2011, 02:01:27 am »
always have this fromat   xxxxx  .  yyyyyyyy

xxxxx = number    dot   yyyyyy=string
?

seba123neo

  • Terabyte
  • *****
  • Mensajes: 763
  • Reputación: +88/-5
    • Ver Perfil
Re:[Problem] What about the problem of sorting a string array
« Respuesta #5 en: Septiembre 18, 2011, 02:31:25 am »

czar9

  • Kilobyte
  • **
  • Mensajes: 64
  • Reputación: +4/-4
    • Ver Perfil
Re:[Problem] What about the problem of sorting a string array
« Respuesta #6 en: Septiembre 18, 2011, 04:52:59 pm »
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


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")

Código: [Seleccionar]
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

Código: [Seleccionar]
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