I want to sort them by LINES, not individual words.
Any thoughts? This is for a school project.
If you are allowed to use STL functions, you could read a file one line at a time with the getline() function and push them into a vector.
The std::sort() function can sort all elements in a vector for you, so you wouldn't have to implement a sort function yourself.
Here's an example that takes in a number of lines and sorts them:
using namespace std;
const int HOWMANY = 4;
for (int i=0; i < HOWMANY; i++)
cout << "Enter word " << i+1 << " of " << HOWMANY << "\n";
cout << "\n\nUnsorted: \n\n";
for (int j= 0; j < HOWMANY; j++)
cout << words[j] << "\n";
cout << "\n\nSorted: \n\n";
for (int k= 0; k < HOWMANY; k++)
cout << words[k] << "\n";
If you aren't allowed to use STL:
Here's a very good link on the art of sorting
You haven't indicated what feature you are going to sort on. Will it be the whole line or will you sort based on a given field within the line. That is will you sort by the firstName field or by the studentNumber or by one of the other fields? If you are sorting by lastname, then you can sort by the entire line. If you are going to sort by one of the other fields you will have to isolate the given fields before you can do the sort. This isolating process is sometimes called parsing.
If you are going to parse the line into fields and then sort on any field but lastName, then you will need some mechanism to keep all of the data in a given line together. A common way to do that is to create an array of user defined objects and sort by one of the variables within the object. To do that you declare the object and define any methods you want to give the user defined type/struct/class such as overloaded =, <<, >>, <, ==, etc operators. Then you parse the lines into individual variables/fields/tokens of a given struct, store the struct in a container, such as a vector as indicated by niek-e, and do the sort.