Sort 2D Vector

This is a discussion on Sort 2D Vector within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Is there a way I can use std::sort() to sort a 2D vector by the value of its ...

  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
    Katy, Texas
    Posts
    2,309
    Code up a comparator routine and pass the routine as a third parm.
    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
    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
    21,456
    If sortVectorStrings is a free function, you would write:
    Code:
    std::sort( outVector.begin(), outVector.end(), sortVectorStrings );
    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

  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,317
    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,046
    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
    Katy, Texas
    Posts
    2,309
    I'd be more likely to help craft a solution if you posted your code, working or not, that was a start.

    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!

  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, 10:40 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