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

``````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 Vector``````

## All 6 Replies

it works right for me
3
2
1
-1
-2
-3

Yes, that is right :pretty: I was just a bit confused I think.

it works right for me
3
2
1
-1
-2
-3

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 ?
I know that the strings is sorted lexicographically by default like above and I am trying to find a way to sort it like below. To mention of importance is that this code is located inside a buttoncontrol on my Form.
I dont know what could be possible to do here. I beleive I could use a struct code like I have attached below, but I am not sure if this could be combined with in this example a buttoncontrol ?

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

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

(Attached struct example)

``````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());``````

I have tried to put a struct like this but this does not compile however. I am not sure if a struct like this could be used in a Form.

``````public ref 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));
}
};``````
``````#include <iostream>
#include <string>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main(int argc, char **argv) {
vector <string> Numbers;
string Number1 = "3,01/05/2000";
string Number2 = "2,01/05/2001";
string Number3 = "1,01/05/2002";
string Number4 = "-3,01/05/2003";
string Number5 = "-2,01/05/2004";
string Number6 = "-1,01/05/2005";

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

vector <pair<int, string*> > lol;
lol.reserve(Numbers.size());
for (size_t i = 0; i < Numbers.size(); ++i) {
stringstream tmp;
tmp << Numbers[i];
int num_tmp;
tmp >> num_tmp;
lol.push_back(pair<int, string*>(num_tmp, &Numbers[i]));
}
bool isChange = true;
while (isChange) {
isChange = false;
for (size_t i = 0; i < lol.size()-1; ++i) {
if (lol[i].first < lol[i+1].first) {
lol[i].first ^= lol[i+1].first ^= lol[i].first ^= lol[i+1].first;
swap(*(lol[i].second), *(lol[i+1].second));
isChange = true;
}
}
}
lol.empty();
for (size_t i = 0; i < Numbers.size(); ++i) {
cout << Numbers[i] << endl;
}
return 0;
}``````

and short version

``````#include <iostream>
#include <string>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main(int argc, char **argv) {
string tmp[] = { "3,01/05/2000", "2,01/05/2001", "1,01/05/2002", "-3,01/05/2003", "-2,01/05/2004", "-1,01/05/2005" };
vector <string> Numbers(tmp, tmp+6);
size_t lol_size = Numbers.size();
int* lol = new int[lol_size];
for (size_t i = 0; i < Numbers.size(); ++i) {
stringstream tmp;
tmp << Numbers[i];
tmp >>lol[i];
}
bool isChange = true;
while (isChange) {
isChange = false;
for (size_t i = 0; i < lol_size-1; ++i) {
if (lol[i] < lol[i+1]) {
lol[i] ^= lol[i+1] ^= lol[i] ^= lol[i+1];
swap(Numbers[i], Numbers[i+1]);
isChange = true;
}
}
}
delete[] lol;
for (size_t i = 0; i < Numbers.size(); ++i) {
cout << Numbers[i] << endl;
}
return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.