Results 1 to 13 of 13

Thread: Sorting words in a string? [Resolved]

  1. #1

    Thread Starter
    Member JesusFreak's Avatar
    Join Date
    Feb 2003
    Location
    Headed for Heaven
    Posts
    57

    Question Sorting words in a string? [Resolved]

    How do you sort words in a string?

    If you have this text:
    this text needs to be sorted

    Then when it is sorted it would turn into:
    be needs sorted text this to
    Last edited by JesusFreak; May 22nd, 2003 at 11:07 AM.
    That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved.

  2. #2
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    I would put the text into a stringreader, then do a SPLIT and SPLIT at each space and insert the items into an array and sort.

    Wrox VB.net text manipulation handbook is a WONDERFUL book..

  3. #3

    Thread Starter
    Member JesusFreak's Avatar
    Join Date
    Feb 2003
    Location
    Headed for Heaven
    Posts
    57

    Thanks, but is there another way?

    Is that the easiest way to do this?
    That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved.

  4. #4
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    Hmm I don't this it's that involved.

    The following is a snippet from WROX VB.net Text Manipulation Handbook.. This is to split

    Dim inputstring as string = "123,abc,456,def"
    dim splitresults as string
    dim string element as string
    splitresults = regex.splut(inputstring, ",")
    for each stringelement in splitresults
    results += stringelement & controlchars.crlf
    next

    Then inside the for each loop you throw it into the array. once in the array then parse the array and then put it back together. Do yourself a HGUE favor and get this book. I recommend it to EVERYONE that needs to manipulate text in vb.net

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Just pass in a string to the SortString method, and you will get a string in return that is sorted. It won't account for every possiblity, but if you have a string of words that is seperated by spaces, it will work just fine.
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         MessageBox.Show(SortString("This is the new string to sort"))
    3.     End Sub
    4.  
    5.  
    6.  
    7.     Private Function SortString(ByVal theString As String) As String
    8.         Dim arrayOfStrings() As String = theString.Split(" ")
    9.         Array.Sort(arrayOfStrings)
    10.         Dim mystrBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
    11.         Dim i As Integer
    12.         For i = 1 To arrayOfStrings.GetUpperBound(0) - 1
    13.             mystrBuilder.Append(arrayOfStrings(i) & " ")
    14.         Next
    15.         Dim returnString As String = mystrBuilder.ToString()
    16.         returnString = returnString.Trim()
    17.         Return returnString
    18.     End Function

  6. #6
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    very nice!

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Thanks, I bet there is even better ways to modify it though to make it more efficient. I just threw it together as an example.

  8. #8

    Thread Starter
    Member JesusFreak's Avatar
    Join Date
    Feb 2003
    Location
    Headed for Heaven
    Posts
    57
    Thanks but I dosn't seem to work properly.
    If I have this string:
    This is the new string to sort
    then I end up with this string:
    new sort string the This

    It's missing two words; is and to.
    That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved.

  9. #9
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    this:
    VB Code:
    1. For i = 1 To arrayOfStrings.GetUpperBound(0) - 1
    2.             mystrBuilder.Append(arrayOfStrings(i) & " ")
    3.         Next
    should have been:
    VB Code:
    1. For i = 0 To arrayOfStrings.GetUpperBound(0)
    2.             mystrBuilder.Append(arrayOfStrings(i) & " ")
    3.         Next

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    duh...lol... that is what I get for hurrying. Thanks for the catch on that one. I don't use VB.Net much, I was thinking it was different than C# in how it dealt with indexes of arrays.

  11. #11
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    C# arrays are 1-based?

  12. #12
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    No, but I thought VB.Net ones were for some reason when I was writing that. Although, I made the mistake on both ends....lol.

  13. #13

    Thread Starter
    Member JesusFreak's Avatar
    Join Date
    Feb 2003
    Location
    Headed for Heaven
    Posts
    57
    It's working good now

    Thanks for all the help!
    That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved.

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