sort a structure

This is a discussion on sort a structure within the C++ Programming forums, part of the General Programming Boards category; I am trying to sort the Structure by author and title when i try to sort it by author it ...

  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
    21,431
    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.
    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

  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
    21,431
    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.
    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

  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
    21,431
    It works with arrays as well.
    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

  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 02:33 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,205
    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 03:57 AM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    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
    21,431
    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.
    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

  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 03:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 08: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, 02: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, 03:54 PM

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