I've finally figured it out, I had to make some significant modifications to the struct I had declared to provide it with some boolean operators (>,<,==,etc) and also add in some constructors for the struct. I also changed the way I was calling the list:: push_back function. Then it was a simple matter to call the default list::sort member function with no arguments. Here is some code.
My new struct:
Code:
struct Sector
{
string strName; // TSV Sector Name
long lPosition; // Address in input file
Sector(const Sector& elem) : strName(elem.strName),
lPosition(elem.lPosition)
{
};
Sector(const string& name, const long position) : strName(name),
lPosition(position)
{
};
Sector& operator= (const Sector& elem)
{
strName = elem.strName;
lPosition = elem.lPosition;
};
bool operator< (const Sector& elem) const
{
return strName < elem.strName;
};
bool operator> (const Sector& elem) const
{
return strName > elem.strName;
};
bool operator== (const Sector& elem) const
{
return strName == elem.strName;
};
};
list<Sector> lstSectors; // Linked-list of "Sector" objects
And the code that adds the stuff to the list:
Code:
int iCount = 0;
string strTemp;
long lPosition;
ifstream input("TsvData.Dat");
// Read through the input file and insert data into the struct/list. ////
input >> strTemp;
while( !input.eof() )
{
// Is this a TSV Sector/SubSector. //////////////////////////////////
if( strTemp[0] == '.' )
{
// Initialize file position from start of "strTemp". /////////////
lPosition = input.tellg() - (long) strTemp.length();
// Add "Sector" to linked list and increment count. ///////////////
lstSectors.push_back( Sector(strTemp,lPosition) );
iCount++;
}
// Output a dot '.' every 100 records. //////////////////////////////
if( iCount % 100 == 0 )
{
cerr << '.';
cerr.flush();
}
// Get the next line of data from the file and continue with the loop.
input >> strTemp;
} // End of "while" loop.
The above loads the list and will be unsorted in terms of the strName data member. The code to sort the linked-list of structs by the strName member is now simply this:Lastly a snippet of data from a file where I dumped the sorted info to show that it was indeed sorted by the string member and not the order that the data was read in:
Code:
Name: .ZSE4201M1 Position: 10680
Name: .ZSE4202M1 Position: 10379
Name: .ZSE4601M1 Position: 9998
Name: .ZSE4701M1 Position: 9495
Name: .ZSE4801M1 Position: 9276
Name: .ZSE9001M1 Position: 8465
Name: .ZSEEU01M1 Position: 6588
Name: .ZSEMF01M1 Position: 7160
Name: .ZSEMW01M1 Position: 7528
Name: .ZSENU01M1 Position: 5340
Name: .ZSENU01M2 Position: 5960
Name: .ZSEPD01M1 Position: 0
Name: .ZSEPD01M2 Position: 688
Name: .ZSEPD01M3 Position: 1370
ygfperson, maybe this can help you if you haven't figured out the answer to that question you asked last week.