1. ## Sort a std::vector<int>

I wonder something about what is happening when I sort this vector. I put some number into the vector like below. After the sort the output look like this.
3
2
1
-3
-2
-1

What I want is that the output instead will look like the below example.
Like below the numbers is sorted from highest to lowest.
How it this possible to do ?
3
2
1
-1
-2
-3

Code:
```std::vector<int> Numbers;
int Number1 = - 1;
int Number2 = - 2;
int Number3 = - 3;
int Number4 =  1;
int Number5 =  2;
int Number6=  3;

Numbers.push_back(Number1);
Numbers.push_back(Number2);
Numbers.push_back(Number3);
Numbers.push_back(Number4);
Numbers.push_back(Number5);
Numbers.push_back(Number6);

std::sort(Numbers.rbegin(), Numbers.rend()); //Sort the Vectorstd::vector<int>```

2. The output is
3
2
1
-1
-2
-3
as expected.

3. Yes, ofcourse you are right. I thought that example should have been the same as the real example that I have below now.
This might then be a bit different. The thing is that I have 6 strings that look like this below that I then put to a std::vector<string> and when I sort this vector, the output will look like this: (Notice the beginning number)

"3,01/05/2000";
"2,01/05/2001";
"1,01/05/2002";
"- 3,01/05/2003";
"- 2,01/05/2004";
"- 1,01/05/2005";

The thing is that I want to sort this in order for the first number like below. Why I want to do this is that I want the date that is after in the string to follow the number.
I dont know if this is possible ?

"3,01/05/2000";
"2,01/05/2001";
"1,01/05/2002";
"- 1,01/05/2005";
"- 2,01/05/2004";
"- 3,01/05/2003";

Code:
```std::vector<string> Numbers;
string Number1 = "- 1,01/05/2000";
string Number2 = "- 2,01/05/2000";
string Number3 = "- 3,01/05/2000";
string Number4 =  "1,01/05/2000";
string Number5 =  "2,01/05/2000";
string Number6 =  "3,01/05/2000";

Numbers.push_back(Number1);
Numbers.push_back(Number2);
Numbers.push_back(Number3);
Numbers.push_back(Number4);
Numbers.push_back(Number5);
Numbers.push_back(Number6);

std::sort(Numbers.rbegin(), Numbers.rend()); //Sort the Vectorstd::vector<int>```

4. sort will sort them as strings, since ... well, since that's what they are. So -1 sorts first, then -2, then -3, then 1, then 2, then 3. You'll need to write your own comparator function and pass that to std::sort. (And since you are writing it yourself, you can make it come out backwards without using reverse iterators! Exciting!)

Note: this will not be the easiest thing ever written, but it shouldn't be too awful.

5. It's possible, but strings are sorted lexicographically by default. You need a custom comparison functor.

For simplicity, I'm assuming the space above is not there. You'll have to do that adaption yourself.
Code:
```struct rcompare_sortkey
{
bool operator()(const std::string &lhs, const std::string &rhs {
std::string::size_type lp = lhs.find(","), rp = rhs.find(",");
return boost::lexical_cast<int>(lhs.substring(0, lp))
> boost::lexical_cast<int>(rhs.substring(0, rp));
}
};

std::sort(Numbers.begin(), Numbers.end(), rcompare_sortkey());```
Note that the functor is a greater-than, so you can use the normal iterators.

6. Thank you ! I will test this and see what I can get out of it.

7. I think I have to ask, do I need to #include something for the boost:: because the compiler doesn&#180;t recognice this.

8. Oh yeah, of course. You need to #include <boost/lexical_cast.hpp> from the Boost libraries.