Thread: Sorting another vector based on how one vector is sorted?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    194

    Sorting another vector based on how one vector is sorted?

    So I have a vector holding numbers and another vector holding strings.
    They both have equal number of elements. The number's elements represent the string's elements. So the number's element and string's element can change indexes but should have the same index eventually.

    Now I have to sort the number's vector in descending order. However, I also have to sort the vector holding strings exactly how I sorted the number's vector.

    Suppose these are the two vectors:
    number's: 1, 54, 32
    string's: God, God's Diapers, Diaper's God

    So after sorting it should be like this:
    number's: 54, 32 ,1
    string's: God's Diapers, Dieaper's God, God


    Makes sense?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Since the data is related have you considered using a structure or class to hold this data?

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by jimblumberg View Post
    Since the data is related have you considered using a structure or class to hold this data?
    Can you give me an example?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If this is an assignment where you're expected to be implementing the sort yourself, then you should write the code to sort vector A and then, every time you do a swap in vector A, you also do the same swap in vector B.

    If this is real life, then these should be in an object of some kind.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nwb View Post
    Can you give me an example?
    Code:
    struct Thing {
        int id;
        std::string name;
    };
    and then write a comparator function that checks the IDs to determine the sorting order.

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    By the way it's not an assignment and I'm not exclusive to any particular method.

    You see, I don't know how many elements the numbers' and strings' vectors have. I get this info at runtime. I form two vectors depending on what the user inputted, such that I have two equally sizes vectors storing the numbers and storing the strings.

    If I could do this with structures or class then I'm clueless.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nwb View Post
    By the way it's not an assignment and I'm not exclusive to any particular method.

    You see, I don't know how many elements the numbers' and strings' vectors have. I get this info at runtime. I form two vectors depending on what the user inputted, such that I have two equally sizes vectors storing the numbers and storing the strings.
    The fact that you don't know how many items there are is orthogonal to the question of how many related items there are. If there is one ID number, corresponding to one string, then that is what your object is.

    The whole point of a vector is to deal with the fact that there are going to be an unknown number of these things. So you define these Things (presumably you would give them a more descriptive name based on what they are), make a std::vector of these Things, push all your items on it, then do a std::sort. (To make the sort work, you'll either need to write a comparator function -- the default is operator<, but since you want things sorted in descending order apparently, I would use a different name.)

  8. #8
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Umgh yes the number is like the ID of the string.

    "So you define these Things" - sorry what do I define? I don't know how to define a structure at run time can you give me an example? And I'm pretty much blanking after that line..

    Okay imagine that I'm a 5 year old. How would you speak to a 5 year old? Yes that's me.


    I interpreted what you said was that you think I should define the structures beforehand and then assign values to them? But then what if I had to support 200 some values.. so that wouldn't work.. and probably that's not what you meant either..
    Last edited by Nwb; 10-31-2018 at 07:53 AM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nwb View Post
    Umgh yes the number is like the ID of the string.

    "So you define these Things" - sorry what do I define? I don't know how to define a structure at run time can you give me an example? And I'm pretty much blanking after that line..
    You can't, but that's okay, because you don't want to. The object is the pair of <id, string> and that's defined right now, not at run time. How were you planning to handle the unknown number of things before? In a vector (well, a pair of vectors). The vectors deal with the quantity of things; the object deals with the relationship between the id and the string.

    Code:
    struct Thing {
        int id;
        std::string name;
    };
    
    bool compare_the_things(const Thing &a, const Thing &b) {
        return a.id > b.id;
    }
    
    int main(void) {
        std::vector<Thing> pile_of_things;
        //however your info is stored you'll have to build the vector
        while(still things to read) {
            Thing temp;
            read in data to temp;
            pile_of_things.push_back(temp);
        }
        //now sort the things
        std::sort(pile_of_things.begin(), pile_of_things.end(), compare_the_things);
        //then you can walk through the list
    }

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    By the way if you're always going to compare the structure one way you could substitute an overload of the operator< in the class instead of the compare() function.

  11. #11
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Ohhh that makes sense. That helps a lot thanks!!

    @jim, I don't get it.. I'm a bit dumb.. so ugh.. I didn't get what you're saying. And yes I will need to compare the structures a lot. I don't get how what when where < is getting overloaded or how to overload it or why to overload it.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Then I suggest you use your favourite search engine and look into C++ operating overloading.

    Edit: By the way what book are you using to learn C++?

  13. #13
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by jimblumberg View Post
    Then I suggest you use your favourite search engine and look into C++ operating overloading.

    Edit: By the way what book are you using to learn C++?
    This might sound strange but I don't have any C++ books or anything I just learnt little bits of C++ from videos online and references here and there.. Do you know if there are any online sources that I can refer? I might buy a book but I'm just really really lazy.

    So jim you're saying I should overload the operater >, but why would I need to do that? And why would it be inside the class? If I did overload >, why would I do that and would I lose the native function of >?
    Last edited by Nwb; 10-31-2018 at 08:36 AM.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    This might sound strange but I don't have any C++ books or anything I just learnt little bits of C++ from videos online and references here and there.
    No this doesn't surprise me since you're jumping all over the place and don't seem to have any real guidance.

    Do you know if there are any online sources that I can refer?
    Not really. I don't believe that most people can really learn a computer language by relying only on on line sources.

    I might buy a book but I'm just really really lazy.
    I suggest you get over this and find and use a book. Any book, even a horribly bad book, will at least have some structure and teach you the basics before you get into more advanced topics. Also talking about lazy, you really need to learn to research your questions before posting to a forum. You can probably find the answers much faster if you do this research before relying on some forum, no matter how many forums you use.

    So jim you're saying I should overload the operater >
    No, I never said that.

    First said operator< not operator>, there is a big difference. Most of the standard algorithms use std::less (the operator<) by default. If you want some other comparison operator you must either use a comparison function (similar to what has already been shown) or some how inform the compiler to use something other than std::less.

    Secondly I also said "you could", not "you should", again big difference.

    And why would it be inside the class?
    I said inside the class because most of the time you want to be able to access any member of the class, not just the public members and operator overloads inside the class are usually easier because they sometimes take fewer parameters. But you can overload some operators outside the class, you just need to insure that you are using the correct parameters.

    If I did overload >, why would I do that and would I lose the native function of >?
    There are no "native" operators for User Defined Types (classes/structures) to lose. If you want an operator you must write the code, again you need to do some research on operator overloading in C++ there is a lot of information available than what I'm willing to write in a forum post.

  15. #15
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Can I get online book copies (umgh I live in the north pole so I don't know if they ship there..)? Do I have to pay (we don't get good wages in the north pole, you see)?

    Okay I clearly have no clue what you're talking about but I'll read about it thanks jim.
    Last edited by Nwb; 10-31-2018 at 10:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Vector of string from another vector of strings
    By subdene in forum C++ Programming
    Replies: 13
    Last Post: 08-03-2016, 09:11 AM
  2. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  3. Sorting vector of structs based on struct member value
    By coffee_cups in forum C++ Programming
    Replies: 19
    Last Post: 06-18-2013, 11:42 AM
  4. ordering vector elements based on heuristic values
    By cyberfish in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2007, 12:33 AM
  5. simple vector-based graphics format?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-25-2006, 11:41 AM

Tags for this Thread