CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Sort a string

  1. #1
    Join Date
    Jun 2001
    Posts
    4

    Sort a string

    Hi all

    I need a function that can sort a string:

    after
    str=SortStr("gduekfhe")
    str should hold "deefghku"

    mvh
    FinnK


  2. #2
    Join Date
    Sep 1999
    Location
    Leeds U.K. (Proud to be Sheffield Born)
    Posts
    202

    Re: Sort a string

    Try this



    Function OrderThis(init)
    Dim str(), tmpstr, vtmpstr
    ReDim str(0)
    ReDim tmp(0)
    tmpstr = init
    While tmpstr <> ""
    ReDim Preserve str(UBound(str) + 1)
    vtmpstr = Left(tmpstr, 1)
    tmpstr = Mid(tmpstr, 2)
    str(UBound(str)) = vtmpstr
    Wend

    Dim team1(), team2()
    ReDim team1(UBound(str))
    ReDim team2(UBound(str))
    Dim gap, doneflag
    Dim index, swap1, swap2

    gap = Int(UBound(str) / 2)
    Do While gap >= 1
    Do
    doneflag = 1
    For index = 1 To UBound(str) - gap
    If UCase(str(index)) > UCase(str(index + gap)) Then
    swap1 = str(index)
    swap2 = team1(index)
    str(index) = str(index + gap)
    team1(index) = team1(index + gap)
    str(index + gap) = swap1
    team1(index + gap) = swap2
    doneflag = 0
    End If
    Next
    Loop Until doneflag = 1
    gap = Int(gap / 2)
    Loop
    For index = 1 To UBound(str)
    OrderThis = OrderThis & str(index)
    Next
    End Function


    Colon-Hyphen-Close-Bracket

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

Featured