So I am trying to sort the column for arrival time so that the earliest arrival time is first. Im new to csv files in java so struggling big time.
I've managed to read the csv file and print using an array but not sure how to sort the specific column
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class readCSV {
public static void main(String[] args) {
String fileName= "csv.csv";
File file= new File(fileName);
// this gives you a 2-dimensional array of strings
List<List<String>> lines = new ArrayList<>();
Scanner inputStream;
try{
inputStream = new Scanner(file);
while(inputStream.hasNext()){
String line= inputStream.next();
String[] values = line.split(",");
// this adds the currently parsed line to the 2-dimensional string array
lines.add(Arrays.asList(values));
//System.out.println(line);
System.out.println(values[0] + ' ' + values[1] + ' ' + values[2] + ' ' + values[3] );
}
inputStream.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
// the following code lets you iterate through the 2-dimensional array
/*int lineNo = 1;
for(List<String> line: lines) {
int columnNo = 1;
for (String value: line) {
System.out.println("Line " + lineNo + " Column " + columnNo + ": " + value);
columnNo++;
}
lineNo++;
}*/
}
}
If there are any other improvements such as storing the csv, printing it etc Im happy to change it
Heres an example of the input:
processID arrivalTime burstTime priority
1 0 5 1
2 1 7 2
3 0 2 1
4 2 6 2
5 6 10 3
6 5 4 4
7 6 4 7
8 5 4 8
9 6 6 3
10 6 7 2
I hope this code down below helps you out :)
What I do, I first create for every row an object.
After that I compare every object with the key of that row I want to compare it with.
That way you can choose your 'CSV key' you want to compare it too.
You can use this code but remember, I put 2 'TODO' in there, those will be possible null pointer exceptions if you don't account with them :)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author runef
*/
public class Test {
public static void main(String[] args) {
new Test();
}
private List<CoolObject> coolObjects = new ArrayList<>();
private String[] linesOfCSV = {"A,B,C,D", "1,2,A,A", "2,1,B,B"};
public Test() {
String[] keys = null;
for (String s : linesOfCSV) {
String[] lineValues = s.split(",");
if (keys == null) {
keys = lineValues;
} else {
Map<String, String> aGoodName = new HashMap<>();
for (int i = 0; i < lineValues.length; i++) {
//TODO: Check if keys[i] exists, check if lineValues[i] exists (if not probs something wrong with CSV)
aGoodName.put(keys[i], lineValues[i]);
}
coolObjects.add(new CoolObject(aGoodName));
}
}
Collections.sort(coolObjects, new SortByKey("A"));
System.out.println("SORTED BY KEY 'A'");
for (CoolObject o : coolObjects) {
for (Map.Entry<String, String> entry : o.getACoolMap().entrySet()) {
System.out.print(entry.getKey() + ": " + entry.getValue() + " ");
}
System.out.print("\n");
}
Collections.sort(coolObjects, new SortByKey("B"));
System.out.println("SORTED BY KEY 'B'");
for (CoolObject o : coolObjects) {
for (Map.Entry<String, String> entry : o.getACoolMap().entrySet()) {
System.out.print(entry.getKey() + ": " + entry.getValue() + " ");
}
System.out.print("\n");
}
}
class CoolObject {
private Map<String, String> aCoolMap;
public CoolObject(Map<String, String> aCoolMap) {
this.aCoolMap = aCoolMap;
}
public Map<String, String> getACoolMap() {
return aCoolMap;
}
}
class SortByKey implements Comparator<CoolObject> {
private String keySorter;
public SortByKey(String keySorter) {
this.keySorter = keySorter;
}
public int compare(CoolObject a, CoolObject b) {
//TODO: CHECK IF KEY EXISTS IN BOTH VALUES! ELSE DO SOMETHING ELSE :) PROBS RETURN -1 SO IT COMES LAST!
return a.getACoolMap().get(this.keySorter).hashCode() - b.getACoolMap().get(this.keySorter).hashCode();
}
}
}