Thread: Creating a function that sorts strings by alphabetic order?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Creating a function that sorts strings by alphabetic order?

    Any good example of how it's done?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm sure there are some on the board, for those willing to search....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    std::string has < and > operators, so I believe the std::sort() function should work. If you want to sort without regards to case, that's another story.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by cpjust View Post
    std::string has < and > operators, so I believe the std::sort() function should work. If you want to sort without regards to case, that's another story.
    Sure, but I want to create the function. Not use one that already exists, for educational purposes.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, implement one of the following:
    Bubble-sort
    Merge-sort
    Quick-sort

    These are googleable, so there's no point in us repeating in less clear form what you can find in for example Wikipedia.

    Sorting strings or numbers or anything else is equivalent if you have a compare operation that can compare two elements of the type you are sorting.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Creating your own version of sort() is pretty simple, especially now that you know std::strings can be compared with < or > or == operators.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by matsp View Post
    So, implement one of the following:
    Bubble-sort
    Merge-sort
    Quick-sort

    These are googleable, so there's no point in us repeating in less clear form what you can find in for example Wikipedia.

    Sorting strings or numbers or anything else is equivalent if you have a compare operation that can compare two elements of the type you are sorting.

    --
    Mats
    Isnt it like in maths, that for example 1>2. But how can you compare A and B? A>B?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Glauber View Post
    Isnt it like in maths, that for example 1>2. But how can you compare A and B? A>B?
    Depends on the object. For strings, A>B is true only if A is lexically greater; that is if A would be found later in the dictionary than B. (assuming that dictionary considers uppercase letters greater then lower case)

    For a truly generic interface, the best way to do it is to have a special comparator function/function object, that is passed as an argument to the sorting function. That way you sorting algorithm can be used to sort the same list of objects by different criteria.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by King Mir View Post
    Depends on the object. For strings, A>B is true only if A is lexically greater; that is if A would be found later in the dictionary than B. (assuming that dictionary considers uppercase letters greater then lower case)

    For a truly generic interface, the best way to do it is to have a special comparator function/function object, that is passed as an argument to the sorting function. That way you sorting algorithm can be used to sort the same list of objects by different criteria.
    Do you have any examples of an "easy" function to sort strings?

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Bubble sort is easy. There's nothing special about sorting strings.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Here's a tip for you;


    string woot = "blabla"

    woot.at(0) == "b"
    woot.at(1) == "l"
    woot.at(2) == "a"
    woot.at(n) == ...

    char letterz = char(woot.at(0)) == 'b'

    basically...

    int somenumber = int(letterz) == 62

    ... c = 63, d = 64 ...


    gl and hf with algorithms ;D
    Last edited by simpleid; 05-27-2008 at 03:30 PM.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by simpleid View Post
    Here's a tip for you;


    string woot = "blabla"

    woot.at(0) == "b"
    woot.at(1) == "l"
    woot.at(2) == "a"
    woot.at(n) == ...

    char letterz = char(woot.at(0)) == 'b'

    basically...

    int somenumber = int(letterz) == 62

    ... c = 63, d = 64 ...


    gl and hf with algorithms ;D
    What if the string is unknown to the program before the input?
    Is there an easier way of sorting strings?

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Can you write a sort for sorting an array of int's? Because std::string supports all the same comparison operators (< > etc), the only thing different is the type of the variables...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    62
    Need another example.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM