Solved

sort csv with multiple columns using Java

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

732 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