Sorting an array of dates as strings

Hello guys, i need help with a specific problem regarding sorting i have run into for my program.

say i have the following text file that contains first and last names, with the third row consisting of dates

Carrie,Williamson,29/06/2028
Alejandro,Bell,07/11/1997
Christina,Wise,27/01/1988
Charlotte,Stephens,14/08/1977
May,Andrews,27/05/1955
Elnora,Porter,03/12/1933
Cory,Morris,19/01/1966
Travis,Thompson,12/05/2070
Lizzie,Holland,10/01/2062
Charlotte,Jensen,09/06/1915
Dennis,Coleman,12/10/2054
Chris,Osborne,03/09/1908
...

the output i get currently would be :

Chris,Osborne,03/09/1908
Elnora,Porter,03/12/1933
Alejandro,Bell,07/11/1997
Charlotte,Jensen,09/06/1915
Lizzie,Holland,10/01/2062
Travis,Thompson,12/05/2070
Dennis,Coleman,12/10/2054
Charlotte,Stephens,14/08/1977
Cory,Morris,19/01/1966
Christina,Wise,27/01/1988
May,Andrews,27/05/1955
Carrie,Williamson,29/06/2028

i want to know if there is a way to sort the last row in this file using the Table function in processing, or if there is another way to sort the last row in this text file

1 Like

as the file is of type
CSV
and there is a

available your idea might be good,
still as

only know column types

|type|int: the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, or STRING

and not DATE
it might not matter too much if you use table or string

the whole line as string ( array of )
and use

on “,/” might give you 5 columns
OR
now the main question is what DATE functions on string are available?
i not find anything snappy so i play:

import java.text.SimpleDateFormat;
import java.util.*;
// string to date format

String indate = "07/11/1997";    // month/day/year thinking
Date makedate = new Date();

Date conv_datestring(String indate) {
  Date pdate = new Date();
  try {
    pdate = new SimpleDateFormat("MM/dd/yyyy").parse(indate);
  }
  catch (Exception e) {
  }
  print(indate+" : ");
  println(new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(pdate));
  
  return pdate;
}


void setup() {
  size(200,200);
  makedate = conv_datestring(indate);
}

can also convert to EPOCH millis ( store as LONG, sort, calc… and back)

long oneday = 1000*24*60*60,onehour = 1000*60*60,datemillis;

void setup() {
  size(200, 200);
  // You can simply parse it to java.util.Date using java.text.SimpleDateFormat 
  // and call it's getTime() function.
  // It will return the number of milliseconds since Jan 01 1970.
  makedate = conv_datestring(indate);
  println("date: "+makedate);
  datemillis = makedate.getTime();                             // can save and sort as LONG
  //println("as Long EPOCH millis "+datemillis);
  datemillis+=oneday+onehour;                                  // calc next day +1 hour
  makedate = new Date(datemillis);                             // convert back
  println("date++: "+makedate);

}

example
// https://discourse.processing.org/t/sorting-an-array-of-dates-as-strings/7487
// not same like
// https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436

/*
Carrie,Williamson,29/06/2028
 Alejandro,Bell,07/11/1997
 Christina,Wise,27/01/1988
 Charlotte,Stephens,14/08/1977
 May,Andrews,27/05/1955
 Elnora,Porter,03/12/1933
 Cory,Morris,19/01/1966
 Travis,Thompson,12/05/2070
 Lizzie,Holland,10/01/2062
 Charlotte,Jensen,09/06/1915
 Dennis,Coleman,12/10/2054
 Chris,Osborne,03/09/1908
 */
// in data/mylist.csv

// get the java date tools
import java.text.SimpleDateFormat;
import java.util.*;
String indate   = "28/11/1997";    // day/month/year thinking
String informat = "dd/MM/yyyy";
String outformat= "yyyy:MM:dd HH:mm:ss";
Date makedate = new Date();

String infilename  = "data/mylist.csv";
String outfilename = "data/mylist_sort.csv";
Table mytable;
TableRow row;
int tcols, trows;
boolean dprint = true;


void getfile_tabledata() {                                   // get tabledata from file
  mytable = loadTable(infilename);//, "header, csv");
  print_table();
}

void make_new_sortcolumn() {
  // idea is that the date are string type date and need to make a new column what convert that date to a standard numeric form what can be sorted.
  mytable.addColumn();
  tcols = mytable.getColumnCount();
  trows = mytable.getRowCount();
  if (dprint) println("new: total cols in table: "+tcols);
  mytable.setColumnType(tcols-1, Table.LONG);                       // for millis sinze 1970 need LONG
  for ( int i = 0; i < trows; i++ ) {
    TableRow thisrow = mytable.getRow(i);
    String datestring = thisrow.getString(2);
    thisrow.setLong(3,conv_datestring_to_millis(datestring,dprint));
  }
}

long conv_datestring_to_millis(String indate, boolean diag) {
  Date pdate = new Date();
  try {
    pdate = new SimpleDateFormat(informat).parse(indate);
  }
  catch (Exception e) {
  }
  if (diag) println(indate+" : "+new SimpleDateFormat(outformat).format(pdate));
  return pdate.getTime();
}

void sort_tabledata(int numcol) {                             // sort numerically a specific column
  //mytable.setColumnType(numcol, Table.INT); 
  mytable.sort(numcol);  
  if (dprint) println("list all rows column "+numcol+" sorted ");
  print_table();
}

void remove_new_sortcolumn(int delcol) {
  mytable.removeColumn(delcol);  
}

void print_table() {
  tcols = mytable.getColumnCount();
  if (dprint) println("total cols in table: "+tcols);
  trows = mytable.getRowCount();
  if (dprint) println("total rows in table: "+trows);

  for ( int i = 0; i < trows; i++ ) {
    TableRow thisrow = mytable.getRow(i);
    if (dprint) print("["+i+"] ");
    for ( int j = 0; j < tcols; j++ ) {      
      if (dprint) print(thisrow.getString(j)+", ");
    }
    if (dprint) println();
  }
}

void save_tabledata() {                               // save to other file
  saveTable(mytable, outfilename);//, "csv");
}

void setup() {
  getfile_tabledata();
  make_new_sortcolumn();                              // column 2 date to column 3 millis 
  sort_tabledata(3);                                  // column sort 0,1,2
  remove_new_sortcolumn(3);
  save_tabledata();
  exit();
}


1 Like