Sorting numbers (file sorting)

Hey guys!!

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
Sorting is a really nice important problem!

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];

pointer_mychar = strtok(pointer_mychar, delimiter);

int i=0;
int pairfind = 0;


while(pointer_mychar != NULL)
{

int CurrentLevel; // current line Level
int PreviousLevel; // previous line Level
string newString;
string contentOfCurrentLevel;
string contentOfPreviousLevel;

if(i=1)
{
Level=pointer_mychar;
getline (Level);
if(CurrentLevel > PreviousLevel)
{
i=3;
newString = contentOfPreviousLevel + contentOfCurrentLevel;
break;
}

else if (CurrentLevel < PreviousLevel)
{
for (PreviousLevel=CurrentLevel-1; PreviousLevel <1; PreviousLevel--)
{
if(CurrentLevel > PreviousLevel)
{
i=3;
newString = contentOfPreviousLevel + contentOfCurrentLevel;
}
}

break;
}

else if (CurrentLevel = PreviousLevel)
{
for (PreviousLevel= CurrentLevel-1; PreviousLevel <1; PreviousLevel--)
{
if(CurrentLevel > PreviousLevel)
{
i=3;
newString = contentOfPreviousLevel + contentOfCurrentLevel;
}
}
break;
}
else
break;
}


if (i>MinColumnSize && i< MaxColumnSize) // Reading column 2-4 only.
{

if (pairfind == 0) {
//shortName = pointer_mychar; // First column is the short Name
Name = newString;
pairfind = 1;
} else if(pairfind == 1) {
Address = pointer_mychar;


pairfind = 2;
}
}
pointer_mychar = strtok(NULL, delimiter);

i++;
}
} else {
break;
}
Last edited on
try this sorting (bubble sort) for strings creating an array of strings...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int x;
    char a;
    string array[x]; //array of strings
    string name;
    cout << "How many names??" << endl;
    cin >> x;
    system("cls");
    int i, j, k;
    for (i=0; i<x; i++) //ketu mbushim array[x] me stringje
    {
        cout << "Enter Name: ";
        cin >> a; //shtova kete qe getline te mos e marri "Enter name" si emer dhe te ne prishi numrin e emrave
        getline(cin,name);
        name= a+name;
        array[i]= name;
          }
          
    for(j=0; j<x-1 ; j++) // bubble sort 
    {
          for(k=j+1; k<x; k++)
          {
                  string temp;
                  if (array[j] > array[k])
                  {
                      temp=array[j];
                      array[j]=array[k];
                      array[k]=temp;
                  }
          }
  }
  cout << endl;

   for (j=0; j<x; j++)
   {
       cout << array[j] << endl;
   }
   cout << endl;
    system("pause");
    return 0;
}
Topic archived. No new replies allowed.