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

1. Do you watch movies of just ask people to describe them to you, scene by scene?

2. Yes, ofcourse. But that is numbers right?

3. Originally Posted by Glauber
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```

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

4. 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. : )

5. Originally Posted by simpleid
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

6. Originally Posted by simpleid
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. Originally Posted by cpjust
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

8. Originally Posted by matsp
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!