# Thread: Problems with sorting

1. ## Problems with sorting

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;
}```

2. You probably want to store your data as a struct, rather than as two individual arrays, and you will automatically solve the problems.

--
Mats

3. I have never understand how struct works. That is really new to me.

I had an explanation on this but did never really understand the logic of this.
Do you know any example/approach how this could be added to this examplecode...

4. Well, it's not really my place to write the code for you, but you could have a struct like this:
Code:
```struct value {
int action;
string symbol;
};```
Then you create a vector of "value", and store a "value" entry for each data-item you have.

--
Mats

5. I am not sure if I understand.
This declaration with the struct, is this happening before or after you get the values from the file and the vector that I will create to store these values, this must still be 2 vectors since one vector can&#180;t contain both int and string at the same time ?
I am really lost on this one.

6. The struct declaration shouldn't be in your main function at all; either in a header file, or at the top of the .c file if you're only using a single source file.

A vector must be of only one type, but that type can be user-defined. In this case, we want a vector of type value.

7. Code:
```struct value {
int action;
string symbol;
};```
So where in the code should this be if not in the main function or above the int main ()?

8. Originally Posted by Coding
Code:
```struct value {
int action;
string symbol;
};```
So where in the code should this be if not in the main function or above this ?
I would recommend between all the lines that say #include and the line that says int main, if you're not using any .h files of your own. So above main (it needs to be there before you try to use any variables of that type).

9. yes the struct is difficult to learn

10. I understand.
Does this meen that I can declare a vector that look like this as I first have declared a struct that is named "Value"
inside int main() ?
Code:
`std::vector<std::vector<Value> > Values(3, std::vector<Value>(10));`
When I refer to action and symbol later in the code. Can I put these to this same vector then ?
Has these int and string variables been declared as something else, like a Value ?

11. We've got a tutorial on structures on the main site, if you're interested.

The syntax is not quite right, I think. You're trying to use 3 as size, and std::vector<Value>(10) as the value for the initializer? That initializer might work, but I wouldn't bother. You can just leave it blank; you'll get uninitialized data to start with, but if you plan to read in each element that won't matter. And you only need one vector:
Code:
`std::vector<Value> Values;`
is a currently empty vector of things of Value type. As you read things in, you can use push_back to add them to the vector. This way you don't have to know how many things are in your file ahead of time.

You access members of structs with dots: a_Value_variable.action. In your case Values is a vector of Value, so Values[0] would be the first Value item, which means you can do Values[0].action and Values[0].symbol.

12. Tabstop,

Thats a good explanation, I think I am close to the solution.
I have tried this code out with all the declarations. But the compiler says that &#180;Symbol&#180; : undeclared identifier.
I beleive I have declared it after the # include.
I will also check the tutorials out..

Code:
```#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>

struct Value
{
int Action;
std::string Symbol;
};

using namespace std;
int main ()
{

char Comma;
int t = 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] = Symbol;
Values[t] = Action;

}

std::sort(Values.begin(), Values.end());

for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++)			//Number of Lines

{
Sort1 <<
Values[Rowsen] << ',' <<
Values[Rowsen]
<< "\n";
}

return 0;
}```

13. Symbol is only defined in the context of a Value struct. Values[t] -- your indexing is off here, by the way, but that's a sidebar -- is a Value hence Values[t].Symbol is defined. Symbol by itself is not.

14. I beleive I have to check out the tutorials. There is somthing I cant understand with the logic. I think I am near to understand but something is missing...

15. That means roughly what it says: if you want to initialize Action2 in that manner, then it must be a static constant -- which means that (1) each Value would share the same copy of it[1] and (2) its value could never change.

This isn't what you want, so don't do that. (You don't need to initialize it anyway, since you shouldn't use it until you read in a value from your file.)

[1]I think. That's what would happen in a class, anyway; I don't know whether it happens that way in a struct.

Popular pages Recent additions