这是我的解决方案,尚未测试:
// this will be our storage of the new z-order
int *tmpZ = new int[GetCount()];
int currentZ = INT_MIN;
int smallestIdx = -1;
int newZ = 0;
for (int passes = 0; passes < GetCount(); passes++)
{
int smallestZ = INT_MAX;
// find the index of the next smallest item
for (int i = 0; i < GetCount(); i++)
{
if (GetAt(i)->m_zOrder > currentZ && GetAt(i) < smallestZ)
{
smallestIdx = i;
smallestZ = GetAt(i)->m_zOrder;
}
}
tmpZ[smallestIdx] = newZ;
// prepare for the next item
currentZ = smallestZ;
newZ++;
smallestIdx = -1;
}
// push the new z-order into the array
for (int i = 0; i < GetCount(); i++)
GetAt(i)->m_zOrder = tmpZ[i];
它是O(n^2),如你所见。。。。:(