I am trying to sort these 2 lines that I have in a .txt file:
ABC,3
DEF,2
I want it to sort it in Ascending order so it will look like this:
DEF,2
ABC,3
With the code below it is doing a sort that look like this(with a blanc space also ?):
ABC,2
DEF,3
One problem that I have is that the first thing I am reading in is a string and the second an int. So I am using 2 different vectors for these, (string and int).
This is why the output look like this. It is logic for me.
So what do I have to add or change in the code to make it look like:
DEF,2
ABC,3
(The sorting will be made on the integers in ascending order)
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int main ()
{
char Comma;
std::string Symbol;
int Action = 0;
int Date1 = 0;
int Date2 = 0;
int Number = 0;
int t = 0;
std::vector<std::vector<int> > Values(3, std::vector<int>(10));
std::vector<std::vector<string> > Values2(1000, std::vector<string>(10));
ofstream Sort1;
Sort1.open ("Sort1.txt");
ifstream Sort ("Sort.txt");
while ( getline(Sort, Symbol, ',') )
{
t = (t + 1); // Count
Sort >> Action; // Buy
{
Values2[t][1] = Symbol;
Values[t][2] = Action;
}
}
std::sort(Values.begin(), Values.end());
for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++) //Number of Lines
{
Sort1 <<
Values2[Rowsen][1] << ',' <<
Values[Rowsen][2]
<< "\n";
}
return 0;
}