Thread: Sort 2D Vector

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    68

    Sort 2D Vector

    Hi guys,

    Is there a way I can use std::sort() to sort a 2D vector by the value of its first member?

    So, for instance:

    Code:
    std::vector< std::vector<char*> > ProductKeyInfo;
    
    ProductKeyInfo.push_back( vector<char*>() );
    ProductKeyInfo[0].push_back( "Microsoft Windows" );
    ProductKeyInfo[0].push_back( "DigitalProductId" );
    ProductKeyInfo[0].push_back( "DecodeMicrosoftKey" );
    
    ProductKeyInfo.push_back( vector<char*>() );
    ProductKeyInfo[1].push_back( "Microsoft Office 2003 - Professional" );
    ProductKeyInfo[1].push_back( "DigitalProductId" );
    ProductKeyInfo[1].push_back( "DecodeMicrosoftKey" );
    I can use std::sort(ProductKeyInfo.begin(), ProductKeyInfo.end()) which sorts by the indexes but can't work out how to sort by the alphabetic order of the strings for the fist member of each. So, i'd like to sort by ProductKeyInfo[i][0] Is there a simple way to do this?

    I guess I could write my own bubble sort method to do this, but I don't want to re-invent the wheel

    Cheers guys!

    Dave

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code up a comparator routine and pass the routine as a third parm.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or make it a struct/class, and implement your own operator< for that, which only uses the field(s) you want to actually sort on.

    I think you will also find that solution more practical for any other code you are using, as you can easily reference the different portions of your struct, rather than indexing [x][1] for the "DigtalProductId", for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks guys.

    I was looking at going down the custom routie option for std::sort(), but am unsure of how to pass the child to the predicate. I've looked arond (honest) but can't find anything suitable.

    I've got it working to sort a 1D vector of strings, how do I pass the members to the sort function though?

    Code:
    std::sort( outVector.begin(), outVector.end(), sortVectorStrings() );
    Thanks,

    Dave

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If sortVectorStrings is a free function, you would write:
    Code:
    std::sort( outVector.begin(), outVector.end(), sortVectorStrings );
    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

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why aren't you using a vector of std::strings?
    Code:
    std::vector< std::string > ProductKeyInfo;

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks guys.

    cpjust - Because a load of APIs I'm calling require char*, so rather than store as a std::string and then convert I figured it best to just store as char* in the first place.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If they require only const char*, then there's c_str().
    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).

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In fact, that should be const char* anyway, since using char* for string literals is deprecated and dangerous. If you never modify those strings then it will probably ok, but it shouldn't hurt to be safer.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And if they require char*, yet do not modify the strings, you can remove the const-ness with a const_cast.

    If they do modify the strings, you're out of luck. For std::strings, anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I'd be more likely to help craft a solution if you posted your code, working or not, that was a start.

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

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks for your input guys, i'm a hobbyist programmer, i'm just going to re-write a section of the code to try and illustrate what i'm trying to achieve rather than post a lot on relavant code

    Thanks for all the input, it's greatly appreciatred!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  3. vector sort
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2005, 10:28 AM
  4. Velocity vector troubles
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 01-18-2005, 11:40 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM