STL Sort on Multidimentional Data: Code
#include <iostream.h>
#include <algorithm>
#include <vector>
using namespace std;
class CSortData
{
public:
char m_szSymbol[5];
long m_lVolume;
double m_dLast;
};
struct Arg2Sort
{
bool operator() (const CSortData & x, const CSortData & y )
{
return x.m_lVolume > y.m_lVolume;
}
};
struct Arg3Sort
{
bool operator() (const CSortData & x, const CSortData & y )
{
return x.m_dLast > y.m_dLast;
}
};
void main()
{
CSortData m_SortData;
vector<CSortData> m_vecSortData;
vector<CSortData>::iterator it;
strcpy(m_SortData.m_szSymbol, "MSFT");
m_SortData.m_lVolume = 28785500;
m_SortData.m_dLast = 55.88;
m_vecSortData.push_back( m_SortData );
strcpy( m_SortData.m_szSymbol, "INTL" );
m_SortData.m_lVolume = 50503100;
m_SortData.m_dLast = 31.44;
m_vecSortData.push_back( m_SortData );
strcpy( m_SortData.m_szSymbol, "CSCO" );
m_SortData.m_lVolume = 100864300;
m_SortData.m_dLast = 26.06;
m_vecSortData.push_back( m_SortData );
cout << "\n" << endl;
cout << " 02/21/01 NASDAQ Data " << endl;
cout << "\n" << endl;
cout << "/******************************" << endl;
cout << "Symbol Volume Close " << endl;
cout << "******************************/" << endl;
cout << "\n" << endl;
for ( it = m_vecSortData.begin(); it != m_vecSortData.end(); it++ )
cout << it->m_szSymbol << "\t"
<< it->m_lVolume << "\t"
<< it->m_dLast << endl;
cout << "\n" << endl;
cout << "/******************************" << endl;
cout << " Sorted By Volume " << endl;
cout << "******************************/" << endl;
cout << "\n" << endl;
std::sort( m_vecSortData.begin(), m_vecSortData.end(), Arg2Sort());
for ( it = m_vecSortData.begin(); it != m_vecSortData.end(); it++ )
cout << it->m_szSymbol << "\t"
<< it->m_lVolume << "\t"
<< it->m_dLast << endl;
cout << "\n" << endl;
cout << "/******************************" << endl;
cout << " Sorted By Close " << endl;
cout << "******************************/" << endl;
cout << "\n" << endl;
std::sort( m_vecSortData.begin(), m_vecSortData.end(), Arg3Sort() );
for ( it = m_vecSortData.begin(); it != m_vecSortData.end(); it++ )
cout << it->m_szSymbol << "\t"
<< it->m_lVolume << "\t"
<< it->m_dLast << endl;
cout << "\n " << endl;
}
Console Output:
02/21/01 NASDAQ Data
/******************************
Symbol Volume Close
******************************/
MSFT 28785500 55.88
INTL 50503100 31.44
CSCO 100864300 26.06
/******************************
Sorted By Volume
******************************/
CSCO 100864300 26.06
INTL 50503100 31.44
MSFT 28785500 55.88
/******************************
Sorted By Close
******************************/
MSFT 28785500 55.88
INTL 50503100 31.44
CSCO 100864300 26.06
(c) 2001, Stan Malevanny