-1

I have a CSV file each line formatted as: 20 char string, 10 char string, 4 char string, and a double.

The goal is to read the file and store the information into a list collection.

then sort using a comparator on the 3rd column and then the 1st column.

What I'd like to know is how to sort them, and whether or not there is an easier way of doing this. ideally the print would be spaced out with something like

System.out.printf("%-20s%-10s%-4s%10s%n", Result.get(0), Result.get(1), Result.get(2), Result.get(3));

Which I attempted, but could not display without needing the "System.out.println(CSVtoArrayList(Line));" line. my current code:

public static void main(String[] args) {

    BufferedReader Buffer = null;

    try {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter file name: ");
        String input1 = scanner.nextLine(); 
        String Line;
        Buffer = new BufferedReader(new FileReader(input1));

        while ((Line = Buffer.readLine()) != null) {
            System.out.println(CSVtoArrayList(Line));

            }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (Buffer != null) Buffer.close();
        } catch (IOException Exception) {
            Exception.printStackTrace();
        }
    }
}


public static ArrayList<String> CSVtoArrayList(String CSV) {
    ArrayList<String> Result = new ArrayList<String>();

    if (CSV != null) {
        String[] splitData = CSV.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;

}

this currently prints out something like:

Enter file name: 
comma.txt
[frist movei, Bob, 1999, 24.98]
[first mobie, Steve, 1999, -345.67]
[first movie, Pam, 1999, 0.00]
[other moive, Sam, 1562, -42.16]
[something, Sue, 8951, 224.62]
Daniel O A
  • 145
  • 7
  • 1
    *"store the information into a list collection"* I see no attempt at doing that. You're asking how to sort a list, but you don't even have a list yet. Take it one step at a time. Once you have a list, do a web search for how to sort a list in Java and you'll find a gazillion examples, and a lot faster than creating a question and waiting on answers. – Andreas Apr 08 '20 at 02:47

1 Answers1

0

i started from scratch, and using code i found:

How to sort csv file by two columns in java?

i achieved the required results

private static final String COLUMN_SEPARATOR = ",";

public static void main(String[] args) throws Exception
{
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter file name: ");
    String input1 = scanner.nextLine(); 
    String csvFile = input1;
    InputStream inputStream = new FileInputStream(input1);
    List<List<String>> lines = readCsv(inputStream);
    Comparator<List<String>> c2 = createAscendingComparator(2);
    Comparator<List<String>> c0 = createAscendingComparator(0);
    Comparator<List<String>> comparator = createComparator(c2, c0);
    Collections.sort(lines, comparator);
    printCsv(lines);        
}

private static List<List<String>> readCsv(
    InputStream inputStream) throws IOException
{
    BufferedReader reader = new BufferedReader(
        new InputStreamReader(inputStream));
    List<List<String>> lines = new ArrayList<List<String>>();

    String line = null;

    while (true)
    {
        line = reader.readLine();
        if (line == null)
        {
            break;
        }
        List<String> list = Arrays.asList(line.split(COLUMN_SEPARATOR));
        lines.add(list);
    }
    return lines;
}

private static void printCsv(List<List<String>> lines) 
    throws IOException
{

    for (List<String> list : lines)
    {
        System.out.printf("%-20s%-10s%-4s%10s%n", list.get(0),list.get(1),list.get(2), list.get(3));

    }

}

private static <T> Comparator<T>
    createComparator(Comparator<? super T>... delegates)
{
    return (t0, t1) -> 
    {
        for (Comparator<? super T> delegate : delegates)
        {
            int n = delegate.compare(t0, t1);
            if (n != 0)
            {
                return n;
            }
        }
        return 0;
    };
}

private static <T extends Comparable<? super T>> Comparator<List<T>>
    createAscendingComparator(int index)
{
    return createListAtIndexComparator(Comparator.naturalOrder(), index);
}

private static <T extends Comparable<? super T>> Comparator<List<T>>
    createAscendingComparator1(int index)
{
    return createListAtIndexComparator(Comparator.naturalOrder(), index);
}

private static <T> Comparator<List<T>>
    createListAtIndexComparator(Comparator<? super T> delegate, int index)
{
    return (list0, list1) -> 
        delegate.compare(list0.get(index), list1.get(index));
}
Daniel O A
  • 145
  • 7