Java sort a csv file based on column date

Arnold Cristobal

Need to sort a csv file based on the date column. This is how the masterRecords array list looks like

GBEP-1-2-4,FRAG,PMTypeEthernet,NEND,TDTN,15-MIN,Dec 15 2014  - 07:15:00 AM MYT,+0,COMPL
GBEP-1-2-1,FRAG,PMTypeEthernet,NEND,TDTN,15-MIN,Dec 15 2014  - 07:00:00 AM MYT,+0,COMPL
GBEP-2-2-1,FRAG,PMTypeEthernet,NEND,TDTN,15-MIN,Dec 15 2014  - 07:30:00 AM MYT,+0,COMPL

I need to sort it out based from the date 07:15:00, 07:30:00, etc. I created a code to sort it out:

// Date is fixed on per 15min interval
ArrayList<String> sortDate = new ArrayList<String>();
    sortDate.add(":00:");
    sortDate.add(":15:");
    sortDate.add(":30:");
    sortDate.add(":45:");

    BufferedWriter bw = new BufferedWriter(new FileWriter(tempPath + filename));

    for (int k = 0; k < sortDate.size(); k++) {
        String date = sortDate.get(k);
        for (int j = 0; j < masterRecords.size(); j++) {
            String[] splitLine = masterRecords.get(j).split(",", -1);
            if (splitLine[10].contains(date)) {
                bw.write(masterRecords.get(j) + System.getProperty("line.separator").replaceAll(String.valueOf((char) 0x0D), ""));
                masterRecords.remove(j);
            }
        }
    }
bw.close();

You can see from above it will loop thru a first array (sortDate) and loop thru again on the second array which is the masterRecord and write it on a new file. It seems to be working as the new file is sorted out but I notice that my masterRecord has 10000 records but after creating a new file the record shrinks to 5000, Im assuming its how I remove the records from the master list. Anyone knows why?

Lautaro Cozzani

Is not safe to remove an item inside of a loop. You have to iterate array over Iterator, for example:

List<String> names = ....
Iterator<String> i = names.iterator();
while (i.hasNext()) {
   String s = i.next(); // must be called before you can call i.remove()
   // Do something
   i.remove();
}

The documentation says:

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JAVA Sort CSV file by columns and then by a date column correctly

Sort CSV file based on first column

;Java - Sort a CSV file by date and time

Sort file based on date using Java

Split a large csv file based on date in First column Python 3.4.3

Sort the CSV file on the basis of date

Java Extract count based on column data from csv file

shell script to print and sort the date column in csv

SQL Query to sort date based on two column

Sort the column based on the name, which contains the date

Sort CSV column values based on headers in perl

Sort elements in a list based on a csv column (Python)

sort the whole .csv based on the value in a certain column

How to split csv file into respective csv files based on date in first column (python)?

Comparing date column of the csv file to today date

How to sort a FASTA file based on date?

How to sort a large FASTA file based on date?

CAT FILE with sort lines by DATE column

Sort a csv file by first column timestamp

Oracle sql sort greater date first, based on 2 date column

How to sort this CSV file by date with the Unix sort command?

Sort a tab delimited file based on column sort command bash

Pandas misinterpreting date column in CSV file

Filter csv file based on extended column values

Split CSV file based on characters in specific column

Splitting csv file based on column value <SOLVED>

Join Two csv file to one Csv file Based matching Column

Sort CSV on date column in Python while ignoring null cell values

Add new column based on a list and sort date by newest