Message 1 of 14
How to sort an array of strings?
Not applicable
05-31-2005
08:23 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I'm doing another arx training, now is about to have a function to return
all the layer names, but I want the output to be sorted, this is what I
have so far:
static addStringToRB (resbuf **work, const char *strVal)
{
resbuf *ptrNewbuf, *ptrPos;
ptrNewbuf = ptrPos = NULL;
ptrPos = *work;
*work = ptrPos;
if (*work)
{
while (ptrPos->rbnext != NULL)
ptrPos = ptrPos->rbnext;
}
if ((ptrNewbuf = acutNewRb(RTSTR)) == NULL)
{
acutPrintf("\nERROR: Unable to allocate memory for resbuf pointer.");
return FALSE;
}
if ((ptrNewbuf->resval.rstring = strdup(strVal)) == NULL)
{
acutPrintf("\nERROR: Unable to duplicate definition in memory.");
acutRelRb(ptrNewbuf);
return FALSE;
}
if (*work)
{
ptrPos->rbnext = ptrNewbuf;
ptrPos = ptrNewbuf;
}
else *work = ptrPos = ptrNewbuf;
return TRUE;
}
static int ads_getlayers(void)
{
resbuf *IsList = NULL;
AcDbLayerTable* pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTbl,
AcDb::kForRead);
AcDbLayerTableIterator* pLayerIterator;
pLayerTbl->newIterator(pLayerIterator);
AcDbLayerTableRecord* pLayerTblRcd;
char *pLName;
// start iterator - not done
for(pLayerIterator->start() ; !pLayerIterator->done() ;
pLayerIterator->step()) {
//for (; !pLayerIterator->done(); pLayerIterator->step()) {
pLayerIterator->getRecord(pLayerTblRcd, AcDb::kForRead);
pLayerTblRcd->getName(pLName);
pLayerTblRcd->close();
//acutPrintf("\nLayer name: %s",pLName);
// adding the layer name into the list
addStringToRB(&IsList, pLName);
// deallocates the memory pointed to by pLName and sets pLName to NULL
acutDelString(pLName);
}
// delete the layer iterator
delete pLayerIterator;
// closing the layer table
pLayerTbl->close();
// if the list is empty [nil] return nil
if (IsList == NULL) acedRetNil();
// if not empty return the result-buffer
I'm doing another arx training, now is about to have a function to return
all the layer names, but I want the output to be sorted, this is what I
have so far:
static addStringToRB (resbuf **work, const char *strVal)
{
resbuf *ptrNewbuf, *ptrPos;
ptrNewbuf = ptrPos = NULL;
ptrPos = *work;
*work = ptrPos;
if (*work)
{
while (ptrPos->rbnext != NULL)
ptrPos = ptrPos->rbnext;
}
if ((ptrNewbuf = acutNewRb(RTSTR)) == NULL)
{
acutPrintf("\nERROR: Unable to allocate memory for resbuf pointer.");
return FALSE;
}
if ((ptrNewbuf->resval.rstring = strdup(strVal)) == NULL)
{
acutPrintf("\nERROR: Unable to duplicate definition in memory.");
acutRelRb(ptrNewbuf);
return FALSE;
}
if (*work)
{
ptrPos->rbnext = ptrNewbuf;
ptrPos = ptrNewbuf;
}
else *work = ptrPos = ptrNewbuf;
return TRUE;
}
static int ads_getlayers(void)
{
resbuf *IsList = NULL;
AcDbLayerTable* pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTbl,
AcDb::kForRead);
AcDbLayerTableIterator* pLayerIterator;
pLayerTbl->newIterator(pLayerIterator);
AcDbLayerTableRecord* pLayerTblRcd;
char *pLName;
// start iterator - not done
for(pLayerIterator->start() ; !pLayerIterator->done() ;
pLayerIterator->step()) {
//for (; !pLayerIterator->done(); pLayerIterator->step()) {
pLayerIterator->getRecord(pLayerTblRcd, AcDb::kForRead);
pLayerTblRcd->getName(pLName);
pLayerTblRcd->close();
//acutPrintf("\nLayer name: %s",pLName);
// adding the layer name into the list
addStringToRB(&IsList, pLName);
// deallocates the memory pointed to by pLName and sets pLName to NULL
acutDelString(pLName);
}
// delete the layer iterator
delete pLayerIterator;
// closing the layer table
pLayerTbl->close();
// if the list is empty [nil] return nil
if (IsList == NULL) acedRetNil();
// if not empty return the result-buffer
and release the memory of the resbuf
else {
acedRetList(IsList);
acutRelRb(IsList);
}
// return the layer list
return (RSRSLT) ;
}
Now what I'm supposed to use in order to have the list of layers [strings]
sorted? I have read about using the following:
//Make a list.
std::vector<:STRING> mylist;
// then inside of the for...
mylist.push_back(pLName);
// Sort the list.
std::sort(mylist.begin(), mylist.end());
But I cannot figure out how to return "mylist" sorted.....
If anyone could help me, please?
Thanks,
Luis.