template < typename T, typename M > struct cmp_mem_var_t
{
    explicit cmp_mem_var_t( M T::*mv ) : mv(mv) {}

    bool operator() ( const T& first, const T& second ) const
    { return first.*mv < second.*mv ; }

    private: M T::*mv ;
};

template < typename T, typename M > // syntactic sugar
cmp_mem_var_t<T,M> compare( M T::*mv ) { return cmp_mem_var_t<T,M>(mv) ; }

template < typename T, typename R > struct cmp_mem_fun_t
{

    explicit cmp_mem_fun_t( R (T::*mfn)() const ) : mfn(mfn) {}

    bool operator() ( const T& first, const T& second ) const
    { return (first.*mfn)() < (second.*mfn)() ; }

    private: R (T::*mfn)() const ;
};

template < typename T, typename R > // syntactic sugar
cmp_mem_fun_t<T,R> compare( R (T::*mfn)() const ) { return cmp_mem_fun_t<T,R>(mfn) ; }

struct A { int n ; double v ; double foo() const { return n+v ; } };

#include <string>
#include <cctype>

struct B
{
    std::string s ;
    std::string bar() const
    {
        std::string t = s ;
        for( char& c : t ) c = std::toupper(c) ;
        return t ;
    }
};

#include <algorithm>
#include <iterator>
#include <iostream>

int main() // simple test driver
{
    A seq_a[] = { { 3, 3.3 }, { 5, 2.2 }, { 2, 6.6 }, { 7, 5.5 }, { 6, 1.1 } } ;
    const auto print_a = [&seq_a] ()
    {
        for( const A& a : seq_a ) std::cout << "A{ " << a.n << ", " << a.v << " }  " ;
        std::cout << '\n' ;
    };
    print_a() ;

    std::sort( std::begin(seq_a), std::end(seq_a), compare(&A::n) ) ;
    print_a() ;

    std::sort( std::begin(seq_a), std::end(seq_a), compare(&A::v) ) ;
    print_a() ;

    std::sort( std::begin(seq_a), std::end(seq_a), compare(&A::foo) ) ;
    print_a() ;

    std::cout << '\n' ;

    B seq_b[] = { { "mno" }, { "ghi" }, { "abc" }, { "JKL" }, { "DEF" } } ;
    const auto print_b = [&seq_b] ()
    {
        for( const B& b : seq_b ) std::cout << "B{ '" << b.s << "' }  " ;
        std::cout << '\n' ;
    };
    print_b() ;

    std::sort( std::begin(seq_b), std::end(seq_b), compare(&B::s) ) ;
    print_b() ;

    std::sort( std::begin(seq_b), std::end(seq_b), compare(&B::bar) ) ;
    print_b() ;
}
