I am trying to do a sort so the contents in a .txt file that look like this:
ABC,3
DEF,2
will look like this: (Ascending order for the number) (The sorting is for the numbers(Called Action2 in my code)
DEF,2
ABC,3
I using stucts to manage this but have a problem to understand what I should do with the these lines that is doing the rules for the sort and then sorting it.
I beleive it should be written "Bool operator" and if the x value is less than the y value inside the parathes Bool evaluates TRUE. I think the logics stands for this.
Then it is many things I dont understand, like if the values x and y is exampleletters or should it stand something else here in my case ?
The next line with return is the same, what should be written instead of x.Values ?
For the sort that coming next.
Should it only be written Values.begin() or do I have to refer to Action2 ?
Code:
bool operator < (const Value& x, const Value& y)
{
return x.Values < y.Values
};
std::sort(Values.begin(), Values.end());
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
struct Value
{
int Action2;
std::string Symbol2;
};
using namespace std;
int main ()
{
char Comma;
int t = 0;
std::string Symbol;
int Action = 0;
std::vector<Value> Values (3);
ofstream Sort1;
Sort1.open ("Sort1.txt");
ifstream Sort ("Sort.txt");
while ( getline(Sort, Symbol, ',') )
{
t = (t + 1); // Count
Sort >> Action; // Buy
Values[t].Symbol2 = Symbol;
Values[t].Action2 = Action;
}
bool operator < (const Value& x, const Value& y)
{
return x.Values < y.Values
};
std::sort(Values.begin(), Values.end());
for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++) //Number of Lines
{
Sort1 <<
Values[Rowsen].Symbol2 << ',' <<
Values[Rowsen].Action2
<< "\n";
}
return 0;
}