Solved

sort csv with multiple columns using Java

Posted on 2014-04-16
4
34 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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