Basic sorting with algorithm library


my currently code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>

using namespace std;

class Books{
private:
	string authorName;
	string title;
	int date , year , month;
public:
	Books();

	void setAuthorName( string );
	void setTitle( string );
	void setDate( int );
	void setMonth( int );
	void setYear ( int );

	int getAuthorName();
	int getTitle();
	int getDate();
	int getMonth();
	int getYear();

};

Books :: Books(){
	authorName = ""; 
	title = ""; 
	date = 0 ; 
	year = 0 ; 
	month = 0 ; 
}


void Books::setAuthorName( string Name ){
	authorName = Name;
}

void Books::setTitle( string theTitle ){
	title = theTitle;
}

void Books :: setDate( int thedate ){
	date = thedate;
}

void Books :: setMonth( int themonth ){
	month = themonth;
}

void Books :: setYear( int theyear ){
	year = theyear;
}


void MainMenu(){
	
	int choice = 0;

	do{
		system( "cls" );
		cout << "----------------------------\n\tDatabase of Books\n---------------------------- " << endl
			<< "1.Add a book's information" << endl
			<< "2.Print an alphabetical list of the books sorted by title" << endl
			<< "3.Quit" << endl << endl
			<< "Enter Choice : ";

		cin  >> choice;
	
		switch( choice ){
		case 1:{
					vector<Books> BookVector;
					bool match = 1;
					string tempName , tempTitle;
					int tempDate = 0 , tempMonth = 0 , tempYear = 0;
					char choice = ' ';

					while( true ){
						system ( "cls" );
						Books tempBook;
					
						cout << "Enter Author Name  : ";
						getline( cin , tempName );
						tempBook.setAuthorName( tempName );

						cout << "Enter Book Title   : ";
						getline( cin , tempTitle );
						tempBook.setTitle( tempTitle );

						cout << "\n\tEnter Publication Date\n---------------------------------" << endl;
						//Skip validation for year since year cannot be validate
						cout << "Year  : ";
						cin  >> tempYear;
						//Do validation & input checking on month
						do{
							cout << "Month : ";
							cin  >> tempMonth;
						}while( tempMonth < 1 || tempMonth > 12 );
						//Do validation for every month and their date
						do{
							cout << "Date   : ";
							cin  >> tempDate;
							if( tempMonth == 2 ){
								if( tempDate < 1 || tempDate > 29 )
									match = false;
							}
							else if ( tempMonth == 1 || tempMonth == 3 || tempMonth == 5 || tempMonth == 7 || tempMonth == 8 
									|| tempMonth == 8 || tempMonth == 10 || tempMonth == 12 )
								if( tempDate < 1 || tempDate > 31 ){
									match = false;
							}
							else if ( tempMonth == 2 || tempMonth == 4 || tempMonth == 6 || tempMonth == 9 || tempMonth == 11 ){
								if( tempDate < 1 || tempDate > 30 )
									match = false;
							}
							cout << endl;
						}while( match );
						tempBook.setDate( tempDate );
						tempBook.setMonth( tempMonth );
						tempBook.setYear( tempYear );

						BookVector.push_back(tempBook);

						cout << "Add Record Successfull " << endl << endl;
						
						cout << "Would you like to add another book? (Y/N) ";
						cin  >> choice;

						if( choice == 'n' || choice == 'N')
							break; //exit while loop, finished adding books.
					}
					getch();
					break;
			   }
		case 2:
			break;
		case 3:
			getch();
			break;
		default:
			cout << "Invalid input ! Please re-enter ! " << endl;
			break;
		}
	}while( choice != 1 || choice != 2 || choice !=3 );
}



. To sort the data, use the generic sort function from the <algorithm> library. Note that this requires you to define the < operator to compare two objects of type Book so that the title field from the two books are compared.
how to sort for the alphabetically for string title?
for the compare two objects of type Book
is mean that using
1
2
Books FirstBook;
Books SecondBook;


or

1
2
 class FirstBook(){};
class SecondBook(){};

hope you all will help .. thanks
Last edited on
Which part is it exactly that you're having trouble with? So far you've shown your code, and told us your assignment. You haven't said what part you want help with.
As question stated above.
just now i didn't not bold it.
Sort the data with algorithm library.
using sort( BookVector.begin() , BookVector.end() ) something like this?
but i don't know how to sort with compare only the Book Title and not the object name . can u see the
compare two objects of type Book
is mean that using
1
2
Books FirstBook;
Books SecondBook;


or

1
2
 class FirstBook(){};
class SecondBook(){};
What & Why
Do you just need the title to be sorted? Do you need to store the sorted data back into the same data set? Do you need the entire record associated with the title to also be sorted?
Normally the DBA controls the data-set and it's related indexes and keys, so that you as a programmer only has to attach the database and access the right key or index and voila, it's done except for printing.

If the sort is not going to be kept/ maintained, then you could simply strip off the Titles and preform a bubble or binary sort on the result set.

If the only requirement will be to Alpha sort the books by Title, then you may wish to remove the option from the menu and insert a call to the sort fnc() within the ADD subrt'n - so that new additions are sorted as they are added.

However, if you wish to expand the sort to include the users input as to how the books are sorted, i.e. by Title, by Author, by Year, by Subject, then your existing menu layout works fine - provided it's expanded for options.

From what I understand you need to sort a vector of your book object by Title. All you need to do is overload the greater-than operator, and then use the sort function from the algorithms library like you would normally.
@script coder. yours 1 i think i more understand about it.
can u just simply to provide a simple code that using
< operator ?
because for me i only know to use this two
friend &ostream operator <<( ostream & a,d object & );

i still haven't use the < operator
can you teach me base on my code ?
or someone might help me also? thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
}#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>

using namespace std;

class Books{
private:
	string authorName;
	string title;
	int date , year , month;
public:
	Books();

	void setAuthorName( string );
	void setTitle( string );
	void setDate( int );
	void setMonth( int );
	void setYear ( int );

	int getAuthorName();
	int getTitle();
	int getDate();
	int getMonth();
	int getYear();

	friend bool operator< ( Books & , Books & );
};

bool operator < ( Books &b1 , Books &b2 ){
	return b1 < b2;
}


but then i need to compare if i have a lot of objects . but for this operator only two objects are allowed to compare? did i implement wrong?
Last edited on
but then i need to compare if i have a lot of objects . but for this operator only two objects are allowed to compare? did i implement wrong?


It should only compare 2 objects at one time. However, you did implement it wrong. You made it a recursive call to itself. Recall that you're supposed to be sorting based on the title of the book.

1
2
3
4
bool operator<(const Books& a, const Books& b)
{
    return a.title < b.title ;
}
Last edited on
@cire thanks you.
by the way

can i pass a vector like this?
1
2
vector<Books> BookVector;
SortingTitle( BookVector );


and when i sorting the title . how to user the generic sort with <algorithm> library instead?
1
2
3
4
5
6
7
void SortingTitle( vector<Books> &BookVector ){

	string tempTitle;
	for( int i = 0 ; i < BookVector.size() ; i++ ){
		BookVector[i].getTitle() < BookVector[i+1].getTitle();
	}
}

mine 1 sure wrong already . mind gv some help or advise? thanks you
1) Yes. That is exactly how you would pass it.

2) If you want to sort it used std::sort, you will need to define an operator < for your Book class so that they can be compared. Once you do that, std::sort should function fine.
1
2
3
4
5
6
friend bool operator< (const Books & ,const Books & );


bool operator < (const Books &a ,const Books &b ){
	return a.title < b.title;
}


ya . i did the bool operator already .
erm .
the sorting is base for my code just that
sorting(BookVector.begin() , BookVector.end() , ??);
what should i put for the ?? since i saw some sources that got put +4 . or + something?
You only need to pass a third argument if you want to use another comparator than operator<.
With the less than operator overloaded, you could use the std::sort() function (defined within the algorithm header) to sort your objects.

 
std::sort(BookVector.begin(), BookVector.end());

** Corrected by Peter87.
Last edited on
There is no need to use a lambda here.
std::sort(BookVector.begin(), BookVector.end());
Last edited on
It sorts it the correct way without it? O.o
Yes.
Topic archived. No new replies allowed.