Thread: sort a vector

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    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());

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Can you use a reverse iterator, rbegin() and rend(), after you have sorted it?

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes that did it. Thanks.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It should be possible to sort using reverse iterators in the first place.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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>());
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM