Im trying to sort an arraylist that was read from a csv file to sort by last name, zip code, and state

Multi tool use
Im trying to sort an arraylist that was read from a csv file to sort by last name, zip code, and state
Here is my code so far it runs but I am stuck as to where to start as far as sorting it. I have tried to make code that sorts the result variable however I have not been able to get any of it to work. Reading and printing the raw data is all I've been able to accomplish so far. Any help would be appreciated.
package csv.read.and.sort;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileWriter;
import java.util.Collections;
/**
*
* @author degen
*/
public class CSVReadAndSort {
public static void main(String args) {
BufferedReader buffread = null;
try {
String line;
buffread = new BufferedReader (new FileReader ("C:UsersdegenOneDriveDocumentsNetBeansProjectsCSV Read and SortsrccsvreadandsortITCO321_U4IP_sample_data.csv"));
// How to read file in java line by line?
while ((line = buffread.readLine()) != null) {
System.out.println("Raw CSV data: " + line);
System.out.println("Converted ArrayList data: " + ITCO321_U4IP_sample_data(line) + "n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffread != null) buffread.close();
} catch (IOException ioexcept) {
ioexcept.printStackTrace();
}
}
}
// Utility which converts CSV to ArrayList using Split Operation
public static ArrayList<String> ITCO321_U4IP_sample_data(String u4ip) {
ArrayList<String> result = new ArrayList<String>();
if (u4ip != null) {
String splitData = u4ip.split("s*,s*");
for (int i = 0; i < splitData.length; i++) {
if (!(splitData[i] == null) || !(splitData[i].length() == 0)) {
result.add(splitData[i].trim());
}
}
}
return result;
}
}
What data does a single line of your csv file represent? In object oriented programming, one would write a class that represents a single dataset, then make it comparable and sortable...
– deHaar
Jul 3 at 6:13
You already know the column number(E.g: column number for lastname, zip code, state etc) on basis of which your need to sort your rows. When you split your row into array, you know if its the 4 element in Array Thus , if column number is 3, you can create a Comparator to compare your two arrays only on the basis of column number 4. The you can simply use Collections.sort(comparator, Array of Arrays (Rows split into array)) to perform the sorting
– akshaya pandey
Jul 3 at 6:22
The other important thing: sorting has nothing to do with "where data is coming from". There is no need to combine those two things in one program. Write one class that reads the CSV file, and that turns it into a helpful data structure (for example a List of objects of a specific class representing the rows). Then, have another class that takes such lists and sorts them. That allows you to develop and test those things independent of each other. And hint: use only those tags that are relevant for your question. That you are using netbeans as IDE doesn't matter at all here!
– GhostCat
Jul 3 at 6:24
Finally: read about java naming conventions. ITCO321_U4IP_sample_data is a really bad name. Why not
readDataFromCsvFile()
instead?! Names should tell the reader what they are about.– GhostCat
Jul 3 at 6:26
readDataFromCsvFile()
1 Answer
1
Since you provide very few informations about the data, let's assume a CSV line contains the follwoing data in this order:
If I got you right ("sorts the result variable" and reffering to the return variable of ITCO321_U4IP_sample_data
named result
), you want to sort this result data to get it in a different order. To achieve this you don't need to sort the result list. Look at the following code:
ITCO321_U4IP_sample_data
result
public static List<String> reorder(List<String> input) {
List<String> reordered = new ArrayList<>(input.size());
reordered.add(input.get(3)); // first name
reordered.add(input.get(0)); // last name
reordered.add(input.get(1)); // zip code
reordered.add(input.get(2)); // state
return reordered;
}
This way you set (copy) every single field of a read CSV line into a new List
in a new sequential order.
List
You could of course use List.sort(...)
to achieve a new order. But his would result in reordering the List
on every moved field. The longer the List and the more fileds to be reordered the less efficient the sorting becomes compared to the simple copying shown above.
List.sort(...)
List
PS: You got already some common hints in the comments. Some additional hints:
It's also good practice to use interface as return types. Applying this to your method you could declare it as follows:
public static List<String> ITCO321_U4IP_sample_data(String u4ip) { ... }
When using resources like a BufferedReader
it's recommended to use the try-with-resource statement. But you can read the whole file in one line of code without to worry about the resource handling using the nio-API this way:
BufferedReader
Files.readAllLines(new File("ITCO321_U4IP_sample_data.csv").toPath());
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
clarify your data by at least a small demo
– Hearen
Jul 3 at 6:12