How to sort a UDT vector in descending order?

#include <bits/stdc++.h>

using namespace std;

class Point
    int x;
    int y;

    Point(int x = 0, int y = 0)
        this->x = x;
        this->y = y;

    bool operator>(const Point &p1)
        return (x + y) > (p1.x + p1.y);

int main()
    vector<Point> v = {{1, 2}, {3, 1}, {0, 1}};

    sort(v.begin(), v.end(), greater<Point>());

    for (auto i : v)
        cout << i.x << " " << i.y << endl;

    return 0;

I want to sort a UDT vector in descending order. So I tried to overload the operator > as written in the class. But it’s giving me error. What should I do to sort the UDT vector in descending order.

This is the error:

In file included from /opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/string:49,
                 from /opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bitset:52,
                 from /opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/x86_64-linux-gnu/bits/stdc++.h:52,
                 from <source>:1:
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_function.h: In instantiation of 'constexpr bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]':
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/predefined_ops.h:158:30:   required from 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<Point*, std::vector<Point> >; _Iterator2 = __gnu_cxx::__normal_iterator<Point*, std::vector<Point> >; _Compare = std::greater<Point>]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:1819:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<Point> >]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:1859:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<Point> >]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:1950:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<greater<Point> >]'
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_algo.h:4893:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Point*, vector<Point> >; _Compare = greater<Point>]'
<source>:27:9:   required from here
/opt/compiler-explorer/gcc-trunk-20221128/include/c++/13.0.0/bits/stl_function.h:398:20: error: no match for 'operator>' (operand types are 'const Point' and 'const Point')
  398 |       { return __x > __y; }
      |                ~~~~^~~~~
<source>:17:10: note: candidate: 'bool Point::operator>(const Point&)' (near match)
   17 |     bool operator>(const Point &p1)
      |          ^~~~~~~~
<source>:17:10: note:   passing 'const Point*' as 'this' argument discards qualifiers

>Solution :

Add the word const to your operator> method so that its signature becomes

bool operator>(const Point &p1) const

instead of

bool operator>(const Point &p1)

Adding const to this method means that you inform the compiler that the method won’t modify the object that the method is associated with. That is required because greater expects the objects being compared to be constant.

Leave a Reply