Solved

sort csv with multiple columns using Java

Posted on 2014-04-16
4
41 Views
Last Modified: 2015-10-24
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
Comment
Question by:ramanjitsingh04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 40005421
Usually - meaning on every occasion - the crew here would need to see some attempt at code from *you* first.
0
 

Author Comment

by:ramanjitsingh04
ID: 40005435
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
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 250 total points
ID: 40005450
Have you looked at implementing the Comparable interface, and then Collections.sort()?
0
 
LVL 3

Accepted Solution

by:
Mark Olsen earned 250 total points
ID: 40009802
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question