title: C++ Reversely sort characters in a string
body: <p>There are tons of codes that sort an array of strings lexicographically but I can't find one that does sorting characters in ONE string reverse lexicographically. So far I discovered that using <code>std::sort()</code> in <code>&lt;algorithm&gt;</code> might be the closest candidate.</p>
  
  <p>This is what I tried:</p>
  
  <pre><code>template &lt;typename T&gt;
  class ReverseComparator{
       bool operator()(T l, T r){return !(l &lt; r);}
  };
  //.....later
  std::sort(str.begin(), str.end(), comparator);
  </code></pre>
  
  <p>Here's the question: how can I initialize my comparator so that it can compare the characters in the string? I tried <code>ReverseComparator&lt;char&gt; comparator</code> but compiler throws a ton of error msg.  <a href="http://ideone.com/ulB623_" rel="nofollow">gcc-4.5.1 showed these errors</a>:</p>
  
  <pre><code>In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
                   from prog.cpp:1:
  prog.cpp: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4:   instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2125:4: error: within this context
  prog.cpp: In function 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7:   instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Size = int, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1914:2: error: within this context
  prog.cpp: In function 'void std::__move_median_first(_Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2260:7:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Size = int, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:108:7: error: within this context
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:110:4: error: within this context
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2260:7:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Size = int, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:112:9: error: within this context
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:115:12: error: within this context
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:117:12: error: within this context
  prog.cpp: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&amp;, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Tp = char, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2261:78:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Size = int, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2229:4: error: within this context
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2232:4: error: within this context
  prog.cpp: In function 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2133:6:   instantiated from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4:   instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2083:7: error: within this context
  In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:62:0,
                   from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63,
                   from prog.cpp:1:
  prog.cpp: In function 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Distance = int, _Tp = char, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:434:4:   instantiated from 'void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1912:7:   instantiated from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7:   instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Size = int, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:303:4: error: within this context
  prog.cpp: In function 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Distance = int, _Tp = char, _Compare = ReverseComparator&lt;char&gt;]':
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:316:7:   instantiated from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Distance = int, _Tp = char, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:434:4:   instantiated from 'void std::make_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1912:7:   instantiated from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5100:7:   instantiated from 'void std::partial_sort(_RAIter, _RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2297:8:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Size = int, _Compare = ReverseComparator&lt;char&gt;]'
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]'
  prog.cpp:12:49:   instantiated from here
  prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private
  /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_heap.h:180:7: error: within this context
  </code></pre>