Any good example of how it's done?
Printable View
Any good example of how it's done?
I'm sure there are some on the board, for those willing to search....
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.
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
Creating your own version of sort() is pretty simple, especially now that you know std::strings can be compared with < or > or == operators.
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.
Bubble sort is easy. There's nothing special about sorting strings.
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
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...
Need another example.