Hi. I’m having some trouble with sorting ArrayLists of Objects. I’m studying the Comprable interface but still have doubts on it’s implementation.
I’m doing a sketch that reads and compile local Covid-19 data. All my data are stored in an open .csv file that is stored on github. the csv files has for each row a timestamp, location(city/state), new cases, total cases, number of deaths. So far so good. I’m reading the tables, storing each entry for the designated place(city,state) and i get a time series visualization for each place.
How is my data stored.
I’ve built a single Object called Entry
class Entry {
String timestamp;
int y;
int m;
int d;
int newCases;
int totalCases;
int numDeaths;
Entry(String timestamp, int y, int m, int d, int nC, int tC, int nD) {
this.timestamp=timestamp;
this.y=y;
this.m=m;
this.d=d;
newCases=nC;
totalCases=tC;
numDeaths=nD;
}
Entry(String timestamp, int y, int m, int d, int nC, int tC) {
this.timestamp=timestamp;
this.y=y;
this.m=m;
this.d=d;
newCases=nC;
totalCases=tC;
}
}
and created two different objects, state and city. each object has it’s own ArrayList of entries and each entry is splitted in different ArrayLists for each data from the entries. below is the data handling parts of the state object, i haven’t placed the graphics handling below.
class Estado {
ArrayList<Entry> entries;
ArrayList<Integer> nCs;//new cases
ArrayList<Integer> tCs;//total cases
ArrayList<Integer> nDs;//num Deaths
ArrayList<String> tSs;//timestamps
int maxTC, maxNC, maxD;//max value for each data
Estado(String sigla, String name, int id, PShape s) {
entries=new ArrayList<Entry>();
nCs=new ArrayList<Integer>();
tCs=new ArrayList<Integer>();
nDs=new ArrayList<Integer>();
tSs=new ArrayList<String>();
pg=createGraphics(graphW, graphH);
this.s=s;
maxTC=0;
maxNC=0;
maxD=0;
this.sigla=sigla;
this.name=name;
this.id=id;
addEntry("0", 0, 0, 0, 0, 0, 0);
}
@Override int compareTo(Estado other){
return this.maxTC - other.maxTC;
}
void addEntry(String ts, int y, int m, int d, int nC, int tC, int nD) {
entries.add(new Entry(ts, y, m, d, nC, tC, nD));
}
void buildData() {
cols=entries.size();
colW=graphW/cols;
for (Entry en : entries) {
int n=en.newCases;
nCs.add(new Integer(n));
int t=en.totalCases;
tCs.add(new Integer(t));
int d=en.numDeaths;
nDs.add(new Integer(d));
String ts=en.d+"/"+en.m;
tSs.add(new String(ts));
if (t>maxTC) maxTC=t;
if (n>maxNC) maxNC=n;
if (d>maxD) maxD=d;
}
}
}
what i want to know is how to sort these objects in descending order accordingly to the desired visualization, total cases and numDeaths by state and top 20 infected cities without interferring with the original array?
tks in advance