problem in list sorting

hello there,

I'm trying to sort a list according to the "length" paramter in ascending order, which represents the third coloumn.
List elements are read from a csv file contains 9 coloumns, I only push 6 coloumns in "futurelist" which is the information i'm intersted in.

//input csv data

1
2
3
4
5
1031	17:11	574.1025391	MB	1050	17:30	19	0	0
1326	22:06	536.0175781	MB	1343	22:23	17	0	0
2721	45:21:00	608.1279297	MB	2741	45:41:00	20	0	0
32	0:32	575.8115234	MB	51	0:51	19	0	0
1161	19:21	652.6259766	MB	1182	19:42	21	0	0



contents of the list before sorting

1
2
3
4
5
1031	574.1025391	1050	19	0	0
1326	536.0175781	1343	17	0	0
2721	608.1279297	2741	20	0	0
32	575.8115234	51	19	0	0
1161	652.6259766	1182	21	0	0





What I want

1
2
3
4
5
1326	536.0175781	1343	17	0	0
1031	574.1025391	1050	19	0	0
32	575.8115234	51	19	0	0
2721	608.1279297	2741	20	0	0
1161	652.6259766	1182	21	0	0





Any body can help me on this, because my code doesn't seem to work. this is my code.


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <sstream>
#include <stdlib.h>
#include <list>


using namespace std;

class FileInfo
{
    public:
    int submissiontime;
    float length;
    int finishtime;
    int remainingtime;
    int actualfinishtime;
    int delay;
};



// create list
list<FileInfo*>futurelist;


//read from excel file
void FileInput()
{
ifstream file ("DownloadList_test.csv"); // declare file stream
string value1;
string value2;
string value3;
string value4;
string value5;
string value6;
string value7;
string value8;
string value9;


for (int t=0;t<5;t++) //read five items only
{
 if (file.good())
{
     getline(file,value1, ','); // read a string until next comma
     getline(file,value2, ',');
     getline(file,value3, ',');
     getline(file,value4, ',');
     getline(file,value5, ',');
     getline(file,value6, ',');
     getline(file,value7, ',');
     getline(file,value8, ',');
     getline(file,value9);


//push values in memory & in future list
FileInfo* fi = new FileInfo();
fi->submissiontime = atoi(value1.c_str());
fi->length= atof(value3.c_str());
fi->finishtime= atoi(value5.c_str());
fi->remainingtime= atoi(value7.c_str());
fi->actualfinishtime= atoi(value8.c_str());
fi->delay= atoi(value9.c_str());

futurelist.push_back(fi);

}
}
file.close();
}



int main()
{

FileInput(); //read csv file

// display contents of future list
cout << "Contents of finished list: ";
list<FileInfo*>::iterator p = futurelist.begin();
while(p != futurelist.end()) {
    cout<<endl;
  cout << (*p)->submissiontime << " ";
  cout << (*p)->length << " ";
  cout << (*p)->finishtime << " ";
  cout << (*p)->remainingtime << " ";
  cout <<(*p)->actualfinishtime<< " ";
  cout <<(*p)->delay<< " ";
    cout<<endl;
p++;
}
cout << "\n\n";

//--------------------------sorting list--------
int temp, i, j, n;
n=futurelist.size();

for(n-1; (n>=1); n--){

    for (int index=0;index<n; index++){
    if(futurelist[index]>futurelist[index+1]) {
    swap(futurelist[index], futurelist[index+1] );
    }
}
}


return 0;

}
Last edited on
Well, the actual question is that my bubble sorting method did not work, because i think it's applicable with arrays, Where can find an example about lists sorting
Topic archived. No new replies allowed.