Solved

sort csv with multiple columns using Java

Posted on 2014-04-16
4
30 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
Comment Utility
Usually - meaning on every occasion - the crew here would need to see some attempt at code from *you* first.
0
 

Author Comment

by:ramanjitsingh04
Comment Utility
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
Comment Utility
Have you looked at implementing the Comparable interface, and then Collections.sort()?
0
 
LVL 3

Accepted Solution

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 55
What is the latest versions eclipse neon 2 113
@SBGen Method 3 25
unix in java example 9 36
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…
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now