AcArray sort

AcArray sort

Anonymous
Not applicable
1,233 Views
5 Replies
Message 1 of 6

AcArray sort

Anonymous
Not applicable
I want to sort some items in a AcArray but it isn't working as expected.

[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"


As you can see, the last element has a starting point of 25. This should not be the last element.

Any suggestions ?

Mike B
0 Likes
1,234 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
No idea of why, there is no way to tell without seeing more of your code.

Here is something that I think you want, HTH.

[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,""
0 Likes
Message 3 of 6

Anonymous
Not applicable
Thanks for the reply. I noticed that you use std::vector, which is OK by me. Instead of using your code, I changed the application to use std::vector container instead of AcArray and it works as expected. Not sure why yet, but it works..

Thanks again.

Mike Bujak
0 Likes
Message 4 of 6

oliver253
Explorer
Explorer
That's because std::sort() expects an iterator to the beginning of the container and another iterator to one past the end of the container, but AcArray::first() and AcArray::last() return references to the first/last element of the array, similar to vector's front() and back(); You can't use STL algorithms directly on an AcArray.
0 Likes
Message 5 of 6

Anonymous
Not applicable
wrote in message news:5639522@discussion.autodesk.com...
>You can't use STL algorithms directly on an AcArray.
Why? It just needs to provide one past end iterator (pointer in this case).

AcArray has also the asArrayPtr() method. And you can use stl algorithms on
arrays 😉
0 Likes
Message 6 of 6

oliver253
Explorer
Explorer
Completely forgot about that one,
std::sort( data.asArrayPtr(), data.asArrayPtr() + data.length() ) should indeed work nicely.