|
[code]
std::sort(&data.first(), &data.last(), SortFileData);
bool SorFileData(const PlotItem& item1, const PlotItem& item2)
{
double x1 = min(item1._start.x, item1._end.x);
double x2 = min(item2._start.x, item2._end.x);
return x1 < x2;
}
[/code]After calling std::sort, the elements are different then without calling sort, but it is still incorrect.
Results:
"T",169.000,11.438,169.000,14.438,"BO"
"P",240.000,42.000,217.000,42.000,""
"T",217.000,11.438,217.000,14.438,"BO"
"P",240.000,6.000,240.000,42.000,""
"T",25.000,11.438,25.000,14.438,"BO"[code]
struct first_numeric_value
{
int ToInt(const std::string& str)
{
int x;
std::stringstream ss(str);
ss >> x;
return x;
}
int getFirstNumericValue(const std::string& StrArray)
{
std::string::size_type pos1 = StrArray.find(",");
std::string::size_type pos2 = StrArray.find(",",pos1+1);
return ToInt(StrArray.substr(pos1+1,pos2-pos1-1));
}
bool operator()(const std::string& lhs, const std::string& rhs)
{
return getFirstNumericValue(lhs) < getFirstNumericValue(rhs);
}
};
static void doSomeTest(void)
{
std::vector< std::string > myLst;
std::vector< std::string >::iterator it;
myLst.push_back( "\"T\",169.000,11.438,169.000,14.438,\"BO\"" );
myLst.push_back( "\"P\",240.000,42.000,217.000,42.000,\"\"" );
myLst.push_back( "\"T\",217.000,11.438,217.000,14.438,\"BO\"" );
myLst.push_back( "\"P\",240.000,6.000,240.000,42.000,\"\"" );
myLst.push_back( "\"T\",25.000,11.438,25.000,14.438,\"BO\"" );
std::sort( myLst.begin(), myLst.end(), first_numeric_value() );
for ( it = myLst.begin(); it != myLst.end(); ++it )
{
std::string s;
s = ( *it );
CString str( s.c_str() );
acutPrintf( _T("\n%s"), str );
}
myLst.clear();
}
[/code]Then it will return an ascending sort of the vector:
"T",25.000,11.438,25.000,14.438,"BO"
"T",169.000,11.438,169.000,14.438,"BO"
"T",217.000,11.438,217.000,14.438,"BO"
"P",240.000,42.000,217.000,42.000,""
"P",240.000,6.000,240.000,42.000,"" |
|