Thread: sort a structure

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    sort a structure

    I am trying to sort the Structure by author and title
    when i try to sort it by author it will only let me sort
    the author and not the title with it.
    so, when i sort by author the title is wrong.
    heres what i have:

    Code:
    struct Library {
    
    string Title;
    string Author;
    };
    
    const int ARRAY_SIZE = 1000;
    Library books [ ARRAY_SIZE ];
    Code:
    void SortByAuthor( int count )
    {
    
    string temp;
       for ( int index = 1; index < count; index++ )
         {
           for( int i = 0; i < count; i++ )
           if(books[i].Author > books[index].Author)
           {
           temp = books[i].Author;
           books[i].Author = books[index].Author;
           books[index].Author = temp;
    
    }
          
    }
    now I would imagine
    I would want to do this:

    Code:
    if(books[i].Author > books[index].Author)
    {
    temp = books[i];
    books[i] = books[index];
    books[index] = temp;
    }
    to copy the whole struct but, i get errors.
    Code:
    1>error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Library' (or there is no acceptable conversion)
    whats wrong?
    thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Are you allowed to use std::sort()?

    Quote Originally Posted by mrsirpoopsalot
    whats wrong?
    You are trying to assign a Library object to a std::string object.
    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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I am allowed to use std::sort()
    what can i assign temp to?
    books temp dont work.

    I will google sort();
    not sure how it works in my case..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by mrsirpoopsalot
    I am allowed to use std::sort()
    Good. Use the version of std::sort() that takes a predicate in addition to the two iterators. You can then implement two predicates: one to compare Library objects by Title, the other to compare Library objects by Author (and come to think of it, perhaps you should rename Library to Book or Publication). These predicates can come in the form of functions that have two Library (const reference) parameters and return a bool, or function objects that overload operator() to have two Library (const reference) parameters and return a bool.
    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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    it looks like std::sort() works with vector.
    we have not learned that yet.
    I dont mind sticking with buble sort.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    It works with arrays as well.
    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

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    i am not really finding anything that helps.
    Last edited by mrsirpoopsalot; 02-26-2009 at 03:33 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    laserlight has givn you an answer in the 4th post of this thread. You'll need to look up the meaning of some of the words in that post, as they are specific to C++ and/or to concepts related to the C++ standard library. However, with a bit of research, you will find that post is spot on.

    Since this is obviously a homework or other learning exercise, you need to do some work to learn.
    Last edited by grumpy; 02-26-2009 at 04:57 AM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Did you change the declaration of 'temp' to 'Library'?

    Also, being pedantic here, but that's not a bubble sort. Bubble sort only swaps adjacent items.
    I call that one Simple sort.

    You use std::sort on an array like this, if you have defined an appropriate less-than operator:
    Code:
    std::sort(books, books+count);

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by iMalc
    You use std::sort on an array like this, if you have defined an appropriate less-than operator:
    However, as I suggested earlier, you should be defining predicates other than a less than operator since you want to sort based on title and author separately. It may not make sense to have a default comparison, but if it does, then defining operator< would be fine.
    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

  11. #11
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I did try assigning temp to library
    ex: library temp; but VS complained.
    what i did eventually get to work using the algorithm header

    Code:
    void SortByTitle( int count )
    {
    
     int CurrIndex, index;
     int smallestindex;
    
       for( int index = 0; index < count; index++ )
          {
          smallestindex = index;
          for( CurrIndex = index + 1; CurrIndex < count; CurrIndex++)
          {
          if(books[CurrIndex].Title < books[smallestindex].Title)
          smallestindex = CurrIndex;
          swap(books[index], books[smallestindex]);
    }
    }
    }
    laserlight thank you for all your help! I think your suggestion was a little over my head.
    I did take what you said into consideration.. Thank you
    Last edited by mrsirpoopsalot; 02-27-2009 at 04:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:35 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM