954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Alphabetically sort lines in text file

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.

BretFelix
Newbie Poster
2 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Yup, you're right.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

BretFelix
Newbie Poster
2 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
(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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

then how can u make it sort within a txt file?

M3gedhom
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
then how can u make it sort within a txt file?


You load it up first.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You