Gwasshoppa
Member
- Joined
- Aug 26, 2011
- Messages
- 7
- Programming Experience
- 5-10
Hi there
I have a CSV file that i want to sort by the second column of data... i.e.
COLUMN 1,COLUMN 2,COLUMN 3,COLUMN 4
1234,54689,55489,The fox
5554,4568,59823,Brush
0578,XYZ,PQRS,12364
I want to be able to write a function that can sort on any one of these columns depending on the index i pass into the function.
i.e. if i pass in index 1 into the function then i need to sort the CSV file data based on COLUMN 2 and thus the data would end up something like this.
COLUMN 1,COLUMN 2,COLUMN 3,COLUMN 4
5554,4568,59823,Brush
1234,54689,55489,The fox
0578,XYZ,PQRS,12364
Note row 1 has now become row 2 because 4568 < 54689 < XYZ
I have looked into using the ArrayList.Sort method but am not really sure what I need to do to make this work.
If I make a custom class using iComparable I just dont seem to understand how I can write this.
Can someone give me some ideas... I can write the data from the text CSV file into the ArrayList no worries... and do a standard MyArray.Sort but how would i write the Custom object to hold my rows of CSV data using the IComparer and IComparable?
Cheers
Gwasshoppa
I have a CSV file that i want to sort by the second column of data... i.e.
COLUMN 1,COLUMN 2,COLUMN 3,COLUMN 4
1234,54689,55489,The fox
5554,4568,59823,Brush
0578,XYZ,PQRS,12364
I want to be able to write a function that can sort on any one of these columns depending on the index i pass into the function.
i.e. if i pass in index 1 into the function then i need to sort the CSV file data based on COLUMN 2 and thus the data would end up something like this.
COLUMN 1,COLUMN 2,COLUMN 3,COLUMN 4
5554,4568,59823,Brush
1234,54689,55489,The fox
0578,XYZ,PQRS,12364
Note row 1 has now become row 2 because 4568 < 54689 < XYZ
I have looked into using the ArrayList.Sort method but am not really sure what I need to do to make this work.
If I make a custom class using iComparable I just dont seem to understand how I can write this.
Can someone give me some ideas... I can write the data from the text CSV file into the ArrayList no worries... and do a standard MyArray.Sort but how would i write the Custom object to hold my rows of CSV data using the IComparer and IComparable?
Cheers
Gwasshoppa