CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Question String sort in Alphabetical order

    Hello,

    Does anyone have an algorithm or know a function that can sort a std::string for me?

    Say i have:

    string one = "test";

    sorting it alfabetically would result in > "estt"

    Thanks,

    Neil.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: String sort in Alphabetical order

    Any numerical sort function should work. Each character is just an integer.

    Viggy

  3. #3
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Re: String sort in Alphabetical order

    Do you have some code? I'm lazy

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: String sort in Alphabetical order

    Nope. Sorry, I don't work for free.

    Viggy

  5. #5
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: String sort in Alphabetical order

    Check out std::sort in <algorithm>, it was just a google away

    http://www.cplusplus.com/reference/algorithm/sort/
    your humble savant

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: String sort in Alphabetical order

    If you are one of those people that refuses to use the std algorithms for whatever reason there are many examples of sorting functions on the internet that you can find, copy, and paste. there is no reason for anyone else on this forum to post code when you can find examples in about 30 seconds.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: String sort in Alphabetical order

    What result do you want for the string "cbaCBA"? In other words, are you looking for a case-invariant sort?

  8. #8
    Join Date
    Jan 2010
    Location
    Netherlands
    Posts
    12

    Re: String sort in Alphabetical order

    Its only caps.

    so for example i need to sort the string "TEST" to "ESTT".

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: String sort in Alphabetical order

    Great, then you don't even need to write a custom comparator for std::sort.

Tags for this Thread

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