-1

My problem is :

I have a CSV file structured like this :

1,"Hello",12.03,17.04

2,"Adam",1.02,13.05

So basically the pattern is :

Integer , String , Double, Float

I have my generic sorting algorithm and I have this CSV file as shown above.

How do I store all those values into an array and make them sort by my Algorithm depending on the field I want to order by?

For example , how do I want order that CSV by the integer field?

I've read about Map,Tokenizer, but i really didn't understand a thing. Is there some goodhearted able to make me understand such a thing?

Community
  • 1
  • 1
Cristiano Soleti
  • 805
  • 7
  • 12
  • how do you intend to sort the data? Do you mean you want to sort the four values stated in your question or do you want to sort several rows that each consist of this data? – ewanc Apr 15 '16 at 09:15
  • I would suggest to use the JDK. 1/ create an object for a row (let's call it Row) of data (the columns become fields), 2/ read the CSV and create a list of Row, 3/ sort the list using a custom Comparator –  Apr 15 '16 at 09:58
  • @ewanc I want to sort every row by a value. For example if i have : Bear,30,51 House,40,50 If i want to sort them by second number it should be House , 40 ,50 Bear,30,51 – Cristiano Soleti Apr 15 '16 at 10:12
  • @RC Thank you , should I go for a : ArrayList row = new ArrayList<>(); --------- ArrayList listOfRows = new ArrayList<>(); ---------- row<- a line of CSV -------- listofRows<- each line --------- And why the comparator thing? – Cristiano Soleti Apr 15 '16 at 10:17
  • No you need a custom object (let's call it Row) and a List. The comparator is a tool used (for example) to sort a list, see http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property –  Apr 15 '16 at 11:00
  • Ok, I'll think about it.. thank you – Cristiano Soleti Apr 15 '16 at 11:02

1 Answers1

2

ok lets break this problem down into a few separate steps, each of which should be fairly easy to solve.

Firstly you need to read in the CSV file. This is nice and simple and there are plenty of tutorials etc for reading files in Java (eg http://www.tutorialspoint.com/java/java_files_io.htm).

Next you need to store this somewhere. This is probably the part that will require the most thought since whatever data structure you use will affect how you sort it. You have stated that you need to store the rows in an array or arraylist (from your comments), an arrayList will be slightly easier as it provides some methods to help with sorting. The rows themselves can be represented in a number of different ways, for example a Map or, as @RC suggests, a custom object.

Finally you need to sort your array (or arrayList). Your question suggests that you already have a sorting algorithm so depending on the interface for this you may be able to pass your unsorted collection directly into it. Alternatively, if using an ArrayList, you can use the sort() method. This requires a Comparator, also as suggested by @RC. This basically just outlines the rules to follow when sorting, and these rules are then applied by the sort() method.

So, long story short, just break the problem down, decide on what data structures to use, and from there you should be able to find plenty of tutorials online to guide you through each individual step.

This seems like a homework question so I don't want to write any code for you, but hopefully this points you in the right direction.

ewanc
  • 1,284
  • 1
  • 12
  • 23
  • This is pretty much what I was looking for, a detailed explanation about it. Thank you sir for your patience! – Cristiano Soleti Apr 15 '16 at 11:40
  • @CristianoSoleti no problem, if you need any more info just let me know – ewanc Apr 15 '16 at 11:42
  • I'll see, with your answer i know what to look for at least. I was kinda confused from google results, too many info, very little neat answers. For now i think i'll follow the CustomObject Class clue , where ill put my compareTo(), and i'll sort them by ArrayList.sort(); – Cristiano Soleti Apr 15 '16 at 11:55