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
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
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
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.
#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; //sort elements according to it
int finishtime;
int remainingtime;
int actualfinishtime;
int delay;
};
// create list
list<FileInfo*>futurelist;
//read from excel file
void FileInput()
{
//DownloadList_test
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;
//int count=0;
//static double sum=0;
//float length;
for (int t=0;t<5;t++) //read five lines only from the csv file
{
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;
}