Creating a function that sorts strings by alphabetic order?

This is a discussion on Creating a function that sorts strings by alphabetic order? within the C++ Programming forums, part of the General Programming Boards category; Do you watch movies of just ask people to describe them to you, scene by scene?...

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Do you watch movies of just ask people to describe them to you, scene by scene?

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    62
    Yes, ofcourse. But that is numbers right?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Glauber View Post
    Yes, ofcourse. But that is numbers right?
    Sorting numbers or sorting strings is the same if you already have an operator > (or an operator <) that works for strings. The std::strings do support that. The way that works is that all characters in the string are actually numbers. So you can see the whole string as a long number (or a sequence of numbers).

    Imagine that you were told to sort cards with a sequence of numbers:
    Code:
    1, 3, 4
    1, 2, 4
    2, 8, 19
    2, 9, 11
    1, 2, 3
    You could do that by comparing each number, and if they are equal, compare the next number.
    So we end up with:
    Code:
    1, 2, 3
    1, 2, 4
    1, 3, 4
    2, 8, 19
    2, 9, 11
    Do you follow that?

    Well, strings are just the same - ok, so different lengths come into it, but if we assume A .. Z are in ascending order in the computers notation of letters (which they will be if you are using a PC), then:
    a < b < c
    aaa < aab < aac < aaz
    a < ab < abc < b < ba

    So we could use exactly the same method to compare strings as "sequnece of numbers". If the characters in the string are not equal, one must be lesser than the other. If the current pair of letters are equal, the go to the next character, until either the end of one or both strings are found (if one string is shorter than the other, the longer is greater than the short). If they are equal all the way through, they are the same content - so neither greater nor lesser.

    Given that std::string already supports compare operations such as <, > and ==, you can use this functionality to sort the numbers exactly the same way you would do when sorting numbers.

    Does that explain better?

    --
    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.

  4. #19
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    edit: corrected error. brain glitches

    my tip did not imply you need to know the string ahead of time.

    the decimal conversion of the letter 'a' is guaranteed to be a lower value than the decimal conversion for the letter 'b' ... and so on.

    (i think guaranteed... i mean i'm talking about general operating systems not any obscure unique cases.)

    you can algorithmically test for the value of every two letter letters through a set of letters in a string, and appropriately sort all of the letters until the entire string is organized.

    or you can break the string up in to an array by spaces and organize them by comparing the first letter of each array element.

    or .... etc.

    is that any clearer? : (

    edit: matsp gave a great and very clear example. : )
    Last edited by simpleid; 05-28-2008 at 07:43 PM.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by simpleid View Post
    my tip did not imply you need to know the string ahead of time.

    the decimal conversion of the letter 'a' is guaranteed to be a higher value than the decimal conversion for the letter 'b' ... and so on.
    Technical pedantery: a is LOWER than b in ASCII, whcih is how you want it if Adam is to come before Bart in an alphabetical listing.

    --
    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. #21
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by simpleid View Post
    the decimal conversion of the letter 'a' is guaranteed to be a higher value than the decimal conversion for the letter 'b' ... and so on.

    (i think guaranteed... i mean i'm talking about general operating systems not any obscure unique cases.)
    Is EBCDIC on z/OS mainframes obscure?

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Is EBCDIC on z/OS mainframes obscure?
    It is rather, actually. How many z/OS (or other IBM mainframe systems for that matter, e.g. Linux of z-series) installations are there world-wide. Compare that to the number of other OS systems there are in the world, and I think you'll find that the z-series system proportion will be rather small.

    --
    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.

  8. #23
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    It is rather, actually. How many z/OS (or other IBM mainframe systems for that matter, e.g. Linux of z-series) installations are there world-wide. Compare that to the number of other OS systems there are in the world, and I think you'll find that the z-series system proportion will be rather small.

    --
    Mats
    Well how many banks & insurance companies are there in the world?
    Most people probably never see a mainframe, but in one way or another, they probably use one, even if it's just by logging onto an ATM machine.
    I would certainly hope the developers at my bank know the difference between ASCII and EBCDIC!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Replies: 26
    Last Post: 07-05-2010, 11: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, 09:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21