I have a program and I want to create a function that sorts multiple lines.
The lines are like this:

lastName,firstName,gender,studentNumber

I want to sort them by LINES, not individual words.

Any thoughts? This is for a school project.

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:

#include<iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() 
{
    const int HOWMANY = 4;
    vector<string> words;
    string oneword;
    for (int i=0; i < HOWMANY; i++)
    {
       cout << "Enter word " << i+1 << " of " << HOWMANY << "\n";
       getline(cin, oneword);
       words.push_back(oneword);
    }

    cout << "\n\nUnsorted: \n\n";
    for (int j= 0; j < HOWMANY; j++)
        cout << words[j] << "\n";

    sort(words.begin(),words.end());

    cout << "\n\nSorted: \n\n";
    for (int k= 0; k < HOWMANY; k++)
        cout << words[k] << "\n";

    cin.get();
    return 0;
}

If you aren't allowed to use STL:
Here's a very good link on the art of sorting

Comments
The link is VERY GOOD :)

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.

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.

For once, I disagree with you :) :

The lines are like this:

lastName,firstName,gender,studentNumber

I want to sort them by LINES, not individual words.

That's why I recommended the getline() function.

thank u everyone!
works great. (though i dont really understand the vectors thingy myself... syntax is good tho)

(though i dont really understand the vectors thingy myself... syntax is good tho)

Tutorial on vectors

A vector is something like an array that has a dynamic size.
The first step is to declare the vector:

vector<string> something

This tells the compiler that I want a vector of strings. You could replace the 'string' by 'int' or whatever.

To fill up the vector the command .pushback is used, which pushes input up the vector.

something.pushback("one");
something.pushback("two");
something.pushback("three");

in vector : one two three Now you can get the values in same way as with arrays; with an indexnumber: cout << something[0] << "\n"; , will output one

This article has been dead for over six months. Start a new discussion instead.