# sort a vector

Printable View

• 02-21-2008
Coding
sort a vector
I use this method to sort a vector and it works good. The sort is in
ascending order like:

15
16
17
etc...

Is it possible using this method in anyway to reverse the order like:

17
16
15
etc...

Code:

```std::vector<string> Sorting; std::sort(Sorting.begin(), Sorting.end());```
• 02-21-2008
Dino
Can you use a reverse iterator, rbegin() and rend(), after you have sorted it?

Todd
• 02-21-2008
Coding
Yes that did it. Thanks.
• 02-21-2008
anon
It should be possible to sort using reverse iterators in the first place.
• 02-21-2008
hk_mp5kpdw
Default sorting criteria uses less-than (<) for comparisons which results in sorted order going from least to greatest. To reverse the order, you can force the sort to use greater-than (>) instead which will result in the order going from greatest to least:
Code:

```#include <functional> ... std::vector<string> Sorting; ... std::sort(Sorting.begin(), Sorting.end(),std::greater<std::string>());```
• 02-21-2008
laserlight
Quote:

Default sorting criteria uses less-than (<) for comparisons which results in sorted order going from least to greatest. To reverse the order, you can force the sort to use greater-than (>) instead which will result in the order going from greatest to least:
Yes, but in this case:
Code:

`std::sort(Sorting.rbegin(), Sorting.rend());`
is good enough.
• 02-21-2008
Daved
>> In this case: std::sort(Sorting.rbegin(), Sorting.rend()); is good enough.

I'd prefer the version with greater. It's easy to overlook the rbegin and rend and assume it's an ascending sort.