Q:

C++ Using different code to handle sorting of vector and list within the same program

I used typedef vector<MyDatatype> ls; to enable switching between list and vector by only changing the typedef definition. All works fine apart from sort function, which is implemented differently for vectors. 
void show(driver& ls)
{
    ls.sort(compare); //this line gives error
    driver::iterator iter=ls.begin();
    int i=1;
    while(iter!=ls.end())
    {
        cout<<i<<". "<<iter->name<<string(maxlength+1-(iter->name).size(),' ');
        streamsize prec=cout.precision(2);
        cout<<grade(*iter);
        cout.precision(prec);
        cout<<endl;
        i++; iter++;
    }
}

Doing something like:
if(typeid(ls)==typeid(list<string>)) ls.sort(compare);
else if(typeid(ls)==typeid(vector<string>)) sort(ls.begin(), ls.end(), compare);

won't do any good. So my question is: is it at all possible to test for type and use suitable sort function accordingly, or do I have to write my own sort function (or maybe convert vector to list)? I couldn't find any related thread.

A:

Use function overloads:
template<typename T, typename C>
void dosort(std::vector<T>& v, C compare) {
    std::sort(ls.begin(), ls.end(), compare);
}

template<typename T, typename C>
void dosort(std::list<T>& v, C compare) {
    ls.sort(compare);
}

void show(ls& list) {
    dosort(list);

    //...
}

A:

The usual approach would be to use function overloading:
template <typename T, typename COMP>
void mySort(std::vector<T>& v, COMP c)
{
  std::sort(v.begin(), v.end(), c);
}

template <typename T, typename COMP>
void mySort(std::list<T>& l, COMP c)
{
  l.sort(c);
}

