I played around with it, because this is an interesting topic, and I have some code that runs on VC++ 7.1 as long as you have Boost downloaded and its include directory (Boost\include\boost-1_32) added to the Additional Include Directory field in the Project Properties.
Code:
#pragma warning(disable: 4512)
#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
#include <boost/variant.hpp>
typedef std::deque<boost::variant<double, std::string> > data_row;
typedef std::deque<data_row> data_grid;
void OutputGrid(const data_grid& the_grid);
class column_sorter
{
public:
column_sorter(data_row::size_type column) : column_(column) { }
bool operator()(const data_row& left, const data_row& right)
{
return left[column_] < right[column_];
}
private:
data_row::size_type column_;
};
int main()
{
// start with 3 empty rows
data_grid the_grid(3);
// Fill those rows with two columns, int and string.
the_grid[0].push_back(37);
the_grid[0].push_back(std::string("thirty-seven"));
the_grid[1].push_back(-1);
the_grid[1].push_back(std::string("negative one"));
the_grid[2].push_back(88);
the_grid[2].push_back(std::string("eighty-eight"));
OutputGrid(the_grid);
// Add two rows with equal string values.
data_row new_row;
new_row.push_back(74);
new_row.push_back(std::string("seventy-four"));
the_grid.push_back(new_row);
new_row[0] = 73;
the_grid.push_back(new_row);
// Insert a row.
new_row[0] = -2;
new_row[1] = std::string("negative two");
the_grid.insert(the_grid.begin() + 2, new_row);
OutputGrid(the_grid);
// Sort by the second column.
std::stable_sort(the_grid.begin(), the_grid.end(), column_sorter(1));
OutputGrid(the_grid);
// Sort by the first column.
std::stable_sort(the_grid.begin(), the_grid.end(), column_sorter(0));
OutputGrid(the_grid);
// Sort by the second column again (notice the 73 and 74 remained the same).
std::stable_sort(the_grid.begin(), the_grid.end(), column_sorter(1));
OutputGrid(the_grid);
std::cin.get();
}
void OutputGrid(const data_grid& the_grid)
{
for (data_grid::const_iterator cur_row = the_grid.begin(),
end_row = the_grid.end();
cur_row != end_row; ++cur_row)
{
for (data_row::const_iterator cur_val = cur_row->begin(),
end_val = cur_row->end();
cur_val != end_val; ++cur_val)
{
if (cur_val != cur_row->begin())
std::cout << ", ";
std::cout << *cur_val;
}
std::cout << std::endl;
}
std::cout << "\n----------------\n" << std::endl;
}