How do i sort and transform a list of objects by different attributes.

Posted on 2003-11-09
Last Modified: 2010-04-01
Hello experts:

I am trying to sort a list of book objects by different attributes, e.g. last name, title, isbn, etc.  My first attempt was to sort by last name in the main program.  The compiler states that the function compare_last_name is an undeclared identifier.

 1.   I have a feeling the parameters in sort function (in the
main program) might be incorrect???
2.  I also want to use the transform function to change to lower case before I sort but am not sure how to do it for a specific data member, e.g. last name, title, etc.
Thank you
#include <string>
using namespace std;

# ifndef BOOK_H
# define BOOK_H

class Book
            Book() { };
            Book(string an_author_first_name, string an_author_last_name,
                   string a_title,string a_publisher, string an_isbn,
                   string a_pub_year, string a_page_count, string a_price,
                   string book_type);
//            ~Book();
            string get_author_first_name() const;
            string get_author_last_name() const;
            string get_title() const;
            string get_publisher() const;
            string get_isbn() const;
            string get_pub_year() const;
            string get_page_count() const;
            string get_price() const;
            string get_book_type()             
            int compare_last_name(Book&);
            int add_book(string isbn, Book a_book);
            int delete_book(string isbn, Book a_book);
            void print_books();

            string author_first_name;
            string author_last_name;
            string title;
            string publisher;
            string isbn;
            string pub_year;
            string page_count;
            string price;
            string book_type;

#include <iostream>
#include <sstream>
#include <iomanip>
#include "book8.h"

using namespace std;

string Book::get_author_first_name() const
      return author_first_name;

int Book::compare_last_name(Book& a_book)
      return author_last_name < a_book.author_last_name ||
               (author_last_name == a_book.author_last_name &&
               author_first_name < a_book.author_first_name) ||
               (author_last_name == a_book.author_last_name &&
               author_first_name == a_book.author_first_name &&
               title < a_book.title);

#include <list>
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include "book8.h"

using namespace std;

void fill_list(list<Book>&, istream&);

template <class T>
inline void print_list(T& L, ostream& outfile, char* optional="")
      typename T::iterator pos;

      outfile << optional << endl;
      cout << optional << endl << endl;
      for (pos = L.begin(); pos != L.end(); ++pos)
            outfile << *pos << ' ';
      outfile << endl;

int main()
      list<Book> book_list;
      ifstream fin;
      ofstream fout;"C:\\CSC311\\Files\\book8_in.txt");
            cout << "Failed to open input file" << endl;

            cout << "Failed to open output file"  << endl;

      fill_list(book_list, fin);
      print_list(book_list, fout, "Book List:\n");
    sort(book_list.begin(), book_list.end(), compare_last_name);
      print_list(book_list, fout, "Sorted Book List by Last Name:\n");
      return 0;

void fill_list(list<Book>& book_list, istream& infile)
      Book a_book;

      while (!infile.eof())
            infile >> a_book;
Question by:deekakes
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Expert Comment

ID: 9710799
You forgot the semicolon after the public: declaration of get_book_type()

         string get_book_type() ;


Author Comment

ID: 9711013
The semicolon didn't quite make it when I copied and pasted the code.
LVL 15

Accepted Solution

efn earned 80 total points
ID: 9711065
> 1.   I have a feeling the parameters in sort function (in the
> main program) might be incorrect???

You're right, the third one is incorrect.  What the sort function wants to see there is binary predicate, a function that takes two parameters and returns a bool value, such as:

bool compare_last_name(const Book& lhs, const Book& rhs);

You could make this function a static Book member function, or make it a free function.  If it's a free function, you will have to make it possible for the function to access the private Book member data you want to use in the comparison.  One way to do that is to declare the function as a friend of the Book class.

> 2.  I also want to use the transform function to change to lower case before I sort but am not sure how to do it for a specific data member, e.g. last name, title, etc.

The fourth argument to transform is a unary function.  In your case, the function would take a Book (or const Book&) as a parameter and return a (transformed) Book.  Within that function, you can do whatever you want, including access members of the argument Book.  Again, you will have to provide a way for the function to access the private members, just as with the function passed to sort.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 9711518

I added the following to my program but am still having problems. (I made it a static member.) I'm pretty sure that one of the parameters should be a_book, but what should the other parameter be?

bool compare_last_name(const Book& a_book, const Book& a_book)
      return author_last_name < a_book.author_last_name ||
               (author_last_name == a_book.author_last_name &&
               author_first_name < a_book.author_first_name) ||
               (author_last_name == a_book.author_last_name &&
               author_first_name == a_book.author_first_name &&
               title < a_book.title);

Assisted Solution

GaryFx earned 50 total points
ID: 9711600
b_book.   Seriously, you need to pass two different Book objects, and as long as you've chosen that naming convention, you may as well stick with it.

By making it a static function, you have eliminated the "this" variable within the body of the method.  Thus you'll need to change the occurrences on the left hand side to include an explicit object reference, e.g.
     b_book.author_last_name < a_book.author_last_name ||
   .... // You can take it from here

Also, if you've really made it a static member, you need to put the class name into the definition, i.e. bool Book::compare_last_name (or is that just a typo, since you had that right the first time)?


Author Comment

ID: 9711654
Thanks....I have spent days trying to figure out a (seemingly) simple thing.....I think I just got to the point where I couldn't think logically!!!  I increased the points: 50 goes to Garyfx and the original 80 goes to efn.

Again, thanks for the help.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 111
I could not set window to top 4 40
How do I get Window Title of all opened process? 4 33
learn programming 8 66
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question