Sort a std::vector<int>

This is a discussion on Sort a std::vector<int> within the C++ Programming forums, part of the General Programming Boards category; I wonder something about what is happening when I sort this vector. I put some number into the vector like ...

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

    Sort a std::vector<int>

    I wonder something about what is happening when I sort this vector. I put some number into the vector like below. After the sort the output look like this.
    3
    2
    1
    -3
    -2
    -1

    What I want is that the output instead will look like the below example.
    Like below the numbers is sorted from highest to lowest.
    How it this possible to do ?
    3
    2
    1
    -1
    -2
    -3


    Code:
    std::vector<int> Numbers;
    int Number1 = - 1;
    int Number2 = - 2;
    int Number3 = - 3;
    int Number4 =  1;
    int Number5 =  2;
    int Number6=  3;
    
    Numbers.push_back(Number1);
    Numbers.push_back(Number2);
    Numbers.push_back(Number3);
    Numbers.push_back(Number4);
    Numbers.push_back(Number5);
    Numbers.push_back(Number6);
    
    std::sort(Numbers.rbegin(), Numbers.rend()); //Sort the Vectorstd::vector<int>

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The output is
    3
    2
    1
    -1
    -2
    -3
    as expected.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes, ofcourse you are right. I thought that example should have been the same as the real example that I have below now.
    This might then be a bit different. The thing is that I have 6 strings that look like this below that I then put to a std::vector<string> and when I sort this vector, the output will look like this: (Notice the beginning number)

    "3,01/05/2000";
    "2,01/05/2001";
    "1,01/05/2002";
    "- 3,01/05/2003";
    "- 2,01/05/2004";
    "- 1,01/05/2005";

    The thing is that I want to sort this in order for the first number like below. Why I want to do this is that I want the date that is after in the string to follow the number.
    I dont know if this is possible ?

    "3,01/05/2000";
    "2,01/05/2001";
    "1,01/05/2002";
    "- 1,01/05/2005";
    "- 2,01/05/2004";
    "- 3,01/05/2003";


    Code:
    std::vector<string> Numbers;
    string Number1 = "- 1,01/05/2000";
    string Number2 = "- 2,01/05/2000";
    string Number3 = "- 3,01/05/2000";
    string Number4 =  "1,01/05/2000";
    string Number5 =  "2,01/05/2000";
    string Number6 =  "3,01/05/2000";
    
    Numbers.push_back(Number1);
    Numbers.push_back(Number2);
    Numbers.push_back(Number3);
    Numbers.push_back(Number4);
    Numbers.push_back(Number5);
    Numbers.push_back(Number6);
    
    std::sort(Numbers.rbegin(), Numbers.rend()); //Sort the Vectorstd::vector<int>
    Last edited by Coding; 04-05-2008 at 03:08 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    sort will sort them as strings, since ... well, since that's what they are. So -1 sorts first, then -2, then -3, then 1, then 2, then 3. You'll need to write your own comparator function and pass that to std::sort. (And since you are writing it yourself, you can make it come out backwards without using reverse iterators! Exciting!)

    Note: this will not be the easiest thing ever written, but it shouldn't be too awful.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's possible, but strings are sorted lexicographically by default. You need a custom comparison functor.

    For simplicity, I'm assuming the space above is not there. You'll have to do that adaption yourself.
    Code:
    struct rcompare_sortkey
    {
      bool operator()(const std::string &lhs, const std::string &rhs {
        std::string::size_type lp = lhs.find(","), rp = rhs.find(",");
        return boost::lexical_cast<int>(lhs.substring(0, lp))
            > boost::lexical_cast<int>(rhs.substring(0, rp));
      }
    };
    
    std::sort(Numbers.begin(), Numbers.end(), rcompare_sortkey());
    Note that the functor is a greater-than, so you can use the normal iterators.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thank you ! I will test this and see what I can get out of it.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I think I have to ask, do I need to #include something for the boost:: because the compiler doesn&#180;t recognice this.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh yeah, of course. You need to #include <boost/lexical_cast.hpp> from the Boost libraries.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 05:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 05:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 01:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM

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