Got a problem I can't solve. I have a table with data in it. To sort these i need the 2nd and 3rd columns. The 2nd columns are the level and 3rd are the names. I want to sort them by the level and then printed out with the attached name eg.
1 name1
2 name2
3 name3
3 name4
3 name5
4 name6 <- the data at no.4 should be sorted 1.2.3.4 and printed out name1.name2.name5.name6
2
3
3
4
4 name11
I was thinking making while and if loops and comparing a row with the row above it. My problem is that i don't know how to print it out this the attached name and also how to sort it like sub items.
Not quite sure if I understand your question, but I would store every row as a struct, giving you a single list to work with. Then you could use for example quicksort to sort that by level
If you just go through and check rows against their neighbours they can only move at most once, if you repeatedly do these conditional swaps until it's all in order then you've just invented bubble sort.
Bubble sort is probably the most simple sorting algorithm, and is considered to have a poor algorithmic time complexity (AKA its really slow), being O(n^2).
But it's fine if you just want the job done and don't care if it's crippled by anything more than a few hundred list items.
As for how to print? Not sure what your issue is: sort the data, iterate over the sorted array and print out the items as you go along.
As for how to display a table nicely? Use tabs \t and clever placement of newlines \n?
The 2nd if loop works and I included the 1st loop to combined the level with the name (like in the example) But the loop isn't working, because we don't have a const value for the current level.
getline(MyFile, String);
if (String != line) // If the line is not empty
{
line = String;
pointer_mychar = &String[0];
int CurrentLevel; // current line Level
int PreviousLevel; // previous line Level
string newString;
string contentOfCurrentLevel;
string contentOfPreviousLevel;
if (pairfind == 0) {
//shortName = pointer_mychar; // First column is the short Name
Name = newString;
pairfind = 1;
} else if(pairfind == 1) {
Address = pointer_mychar;