sort a vector

This is a discussion on sort a vector within the C++ Programming forums, part of the General Programming Boards category; I use this method to sort a vector and it works good. The sort is in ascending order like: 15 ...

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

    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
    Katy, Texas
    Posts
    2,309
    Can you use a reverse iterator, rbegin() and rend(), after you have sorted it?

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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,799
    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
    21,594
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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, 04:21 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 09:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21