Results 1 to 7 of 7

Thread: [RESOLVED] Sort String

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Resolved [RESOLVED] Sort String

    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

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Sort String

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: [RESOLVED] Sort String

    thank you very very much

  4. #4
    New Member Gaspare's Avatar
    Join Date
    Sep 2013
    Posts
    6

    Re: [RESOLVED] Sort String

    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 )

  5. #5
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: [RESOLVED] Sort String

    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.

  6. #6
    New Member Gaspare's Avatar
    Join Date
    Sep 2013
    Posts
    6

    Re: [RESOLVED] Sort String

    mcoulter876 have you translate my post? why?

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Sort String

    Quote Originally Posted by Gaspare View Post
    mcoulter876 have you translate my post? why?
    This is an English language forum only. Also, since you are using VB2005 you should post in the VB.NET section.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width