• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

sort csv with multiple columns using Java

I have a file with a header and multiple rows. I need a way to read the file and sort it based of SIDE, REF1, Ref2,Account or in another order, defined by the header. I don't have the full java skills to write the sorting code. I need to do it in java because I'm doing another process for validating other info from the file.

BEFORE:
SIDE|REF1|REF2|ACCOUNT|AMOUNT
L|ABCD|1234|12KL2SL2312|24.99
S|JSKS|2321|DAS9DA0S9DS|10.99
L|ABCD|1235|12KL2SL2312|23.99
L|ABCD|0009|1029S0SJ2|2.99

AFTER:
SIDE|REF1|REF2|ACCOUNT|AMOUNT
L|ABCD|0009|1029S0SJ2|2.99
L|ABCD|1234|12KL2SL2312|24.99
L|ABCD|1235|12KL2SL2312|23.99
S|JSKS|2321|DAS9DA0S9DS|10.99
0
ramanjitsingh04
Asked:
ramanjitsingh04
  • 2
2 Solutions
 
krakatoaCommented:
Usually - meaning on every occasion - the crew here would need to see some attempt at code from *you* first.
0
 
ramanjitsingh04Author Commented:
Well truthfully I'm researching what I can... But I'm getting odd solutions. I'm hoping someone can help guide me here and can point me in a direction. The example I gave above is a smaller version of a larger file. The sorting has to happen up to 8 different columns.

I was considering a arraylist for the whole string, and using a string as a key column for the array and then using collection.sort to sort the key and then using CSVwritter to print to a file. But I don't understand the collection.sort and how to use the arraylist as a key.
0
 
krakatoaCommented:
Have you looked at implementing the Comparable interface, and then Collections.sort()?
0
 
Mark OlsenCommented:
Going with an object oriented approach I would make an object that represents each row with a field for each column. Then make objects that extend the Comparator interface for each column.

When reading in the header of the file the code would need decide which comparator to use. Once the data has been read and fed into a Collection, you then call Collections.sort() and pass in the comparator.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now