# Creating a function that sorts strings by alphabetic order?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-27-2008
Glauber
Creating a function that sorts strings by alphabetic order?
Any good example of how it's done?
• 05-27-2008
Salem
I'm sure there are some on the board, for those willing to search....
• 05-27-2008
cpjust
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.
• 05-27-2008
Glauber
Quote:

Originally Posted by cpjust
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.
• 05-27-2008
matsp
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
• 05-27-2008
cpjust
Creating your own version of sort() is pretty simple, especially now that you know std::strings can be compared with < or > or == operators.
• 05-27-2008
Glauber
Quote:

Originally Posted by matsp
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?
• 05-27-2008
King Mir
Quote:

Originally Posted by Glauber
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.
• 05-27-2008
Glauber
Quote:

Originally Posted by King Mir
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?
• 05-27-2008
King Mir
Bubble sort is easy. There's nothing special about sorting strings.
• 05-27-2008
simpleid
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
• 05-27-2008
Glauber
Quote:

Originally Posted by simpleid
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?
• 05-27-2008
anon
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...
• 05-27-2008
Glauber
Need another example.
• 05-27-2008
cpjust
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last