H I am trying to sort a text file either based on the first or second. So I would ask the user whether they want to sort by the first or second then it created a new txt file sorted based on what the user wants. But I can't figure out how and am having trouble with this.
From what I have it prints the students name but the same topic
Student1 A
Student1 A
Student1 A
Student1 A
Student1 A
Student1 A
Student1 A
Student1 A
even though students a's topics should vary it doesn't change it
@lastchanve hey thanks
ifstream in( "dailylog.txt" );
ofstream output("output.txt");
can I use these in that code because its a text file that changes everytime i run the code its supposed to be a simulation and I'm trying to sort it?
I deleted my previous post, didn't test but if you want to sort on both a combination with stable partition should do the trick.
Again, I didn't test this, but a bit of modifications should give you what you want.
Note that using std::sort twice would not work because it would negate first run.
Yes, you can go back to the filestreams - I only commented them out because you can't handle (permanent) input and output files with the online compiler.
Given the repeated values in both first and second, it looks like the comparison should probably account for both first and then second (or second and then first.)
You can indeed sort descending by switching < to >.
@andywestken is right: where the key that you are sorting on is actually equal in two pairs it would make sense to have a second-level sort as well. At this point I think I would be writing a "full" function as predicate, rather than a lambda function, because the latter seems to work best for short, simple statements.
It will put student20 before student3 because you are sorting alphabetically. If you want to do otherwise you would have to split string from numerical value. But you aren't seriously calling them Student1, Student2, etc., are you? You might just as well call them 1, 2, 3, ...
At the end of the day, write "predicate" functions to determine which of your items "come first" in the list. These functions become the third argument to std::sort().