Results 1 to 6 of 6

Thread: [RESOLVED] How to sort string?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Resolved [RESOLVED] How to sort string?

    Dear all expert,
    Please tell me how to sort string from "9874056213" to "0123456789".

    Thank you for all post.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to sort string?

    Read each character and build a new string with the lowest character first etc. Use Mid$ and Instr in a loop to read the characters.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to sort string?

    Code:
    Const TESTDATA = "9874056213"
    Dim strNew As String
    Dim lngIndex As Long
    Dim lngIndexNew As Long
    
    strNew = Mid$(TESTDATA, 1, 1)
    lngIndex = 2
    Do Until lngIndex = Len(TESTDATA) + 1
        For lngIndexNew = 1 To Len(strNew)
            If Mid$(TESTDATA, lngIndex, 1) < Mid$(strNew, lngIndexNew, 1) Then
                If lngIndexNew = 1 Then
                    ' Add the new character at the beginning
                    strNew = Mid$(TESTDATA, lngIndex, 1) & strNew
                Else
                    ' Stick it in the middle somewhere
                    strNew = Left$(strNew, lngIndexNew - 1) & Mid$(TESTDATA, lngIndex, 1) & Mid$(strNew, lngIndexNew)
                End If
                lngIndex = lngIndex + 1
                Exit For
            End If
        Next
    Loop

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to sort string?

    Thank you for your code. I modify your code to function and test it.

    vb Code:
    1. Private Function SortString(ByVal strSource As String) As String
    2.     Dim strNew As String
    3.     Dim lngIndex As Long
    4.     Dim lngIndexNew As Long
    5.     strNew = Mid$(strSource, 1, 1)
    6.     lngIndex = 2
    7.     Do Until lngIndex = Len(strSource) + 1
    8.         For lngIndexNew = 1 To Len(strNew)
    9.             If Mid$(strSource, lngIndex, 1) < Mid$(strNew, lngIndexNew, 1) Then
    10.                 If lngIndexNew = 1 Then
    11.                     ' Add the new character at the beginning
    12.                     strNew = Mid$(strSource, lngIndex, 1) & strNew
    13.                 Else
    14.                     ' Stick it in the middle somewhere
    15.                     strNew = Left$(strNew, lngIndexNew - 1) & Mid$(strSource, lngIndex, 1) & Mid$(strNew, lngIndexNew)
    16.                 End If
    17.                 lngIndex = lngIndex + 1
    18.                 Exit For
    19.             End If
    20.         Next
    21.     Loop
    22.     SortString = strNew
    23. End Function
    24.  
    25. Private Sub Command1_Click()
    26.     MsgBox SortString("9874056213")
    27. End Sub

    If choose MsgBox SortString("9874056213") it worked.
    If choose MsgBox SortString("3152") it is not worked.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to sort string?

    Code:
    'Const TESTDATA = "9874056213"
    Const TESTDATA = "3152"
    Dim strNew As String
    Dim lngIndex As Long
    Dim lngIndexNew As Long
    Dim bInserted As Boolean
    
    strNew = Mid$(TESTDATA, 1, 1)
    lngIndex = 2
    Do Until lngIndex = Len(TESTDATA) + 1
        bInserted = False
        For lngIndexNew = 1 To Len(strNew)
            If Mid$(TESTDATA, lngIndex, 1) < Mid$(strNew, lngIndexNew, 1) Then
                If lngIndexNew = 1 Then
                    ' Add the new character at the beginning
                    strNew = Mid$(TESTDATA, lngIndex, 1) & strNew
                    bInserted = True
                Else
                    ' Stick it in the middle somewhere
                    strNew = Left$(strNew, lngIndexNew - 1) & Mid$(TESTDATA, lngIndex, 1) & Mid$(strNew, lngIndexNew)
                    bInserted = True
                End If
                lngIndex = lngIndex + 1
                Exit For
            End If
        Next
        If Not bInserted Then
             strNew = strNew & Mid$(TESTDATA, lngIndex, 1)
            lngIndex = lngIndex + 1
        End If
    Loop

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: How to sort string?

    Thank you very much.

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