Sort Rational Numbers

  1. Suppose you have the definition of class Rational:
    
    #include <iostream>
    #include <stdexcept>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    using std::vector;
    using std::invalid_argument;
    using std::ostream_iterator;
    using std::ostream;
    using std::cout;
    
    class Rational
    {
    friend ostream& operator<<(ostream& output, const Rational& b) {
        output << b.numerator;
        if (b.denominator > 1)
            output << '/' << b.denominator;
        return output;
        }
    
    public:
        Rational(int n=0, int d=1): numerator(n), denominator(d) {
            if (d==0)
                throw( invalid_argument("Divide by Zero") );
        }
        int getNumerator() { return numerator; }
        int getDenominator() { return denominator; }
    protected:
        int numerator;
        int denominator;
    
    };
    
    int main()
    {
        ostream_iterator<Rational> output( cout, " " );
        vector<Rational> a;
        for (int i=1; i<6; ++i)
            for (int j=1; j<4; ++j)
                a.push_back(Rational(i, j));
        copy(a.begin(), a.end(), output);
        cout << '\n';
    }  
  2. Now at the end of the program, we want to add three lines to sort these rational numbers:
        sort(a.begin(), a.end(), f);
        copy(a.begin(), a.end(), output);
        cout << '\n';
    
    
  3. How should you define the function f() so that the sort algorithm knows how to compare two Rational correctly?