[cs23021] sorting in descending order

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Apr 2 12:44:52 EDT 2012


CSI students,

Regarding today's question on sorting elements in a vector in
descending order. The best way to do it, is to use the more general
version of "sort" function, explicitly specifying what to use to
compare elements for sorting. Any comparison function can be used that
takes two arguments from the vector base type and return a boolean
value. A particularly convenient function would be an already defined
function "greater". The descending sorting example would be:

 vector<int> myvector;
 ...
 sort(myvector.begin(),myvector.end(),greater<int>());

Thanks,
-- 
Mikhail


More information about the cs23021-2 mailing list