Sorting ArrayList of Objects

Okay, so you have an Estado and a Ciudade Cidade class, and each of them contains an ArrayList<Entry>. Is that right?

When you say

i want to know is how to sort these objects in descending order accordingly to the desired visualization

Does this mean that you have an ArrayList<Estado> that you want to sort? And you want them sorted by new cases from… the most recent entry, or a specific timestamp, or the largest historical entry, or…?

@quark is right that you can drop these:

  ArrayList<Integer> nCs;//new cases
  ArrayList<Integer> tCs;//total cases
  ArrayList<Integer> nDs;//num Deaths
  ArrayList<String> tSs;//timestamps

The idea here will be to implement Comparators.

ArrayList<Entry> myEntries;
Comparator<Entry> entryCompByNew;
Comparator<Entry> entryCompByTotal;
Comparator<Entry> entryCompByDeaths;
Comparator<Entry> entryCompByTime;

myEntries.sort(entryCompByNew);

Then for Estados, the same thing:

Comparator<Estado> estadoCompByLatestNew;
Comparator<Estado> estadoCompByLatestTotal;
Comparator<Estado> estadoCompByLatestDeaths;
Comparator<Estado> estadoCompByTopNew;
Comparator<Estado> estadoCompByTopTotal;
Comparator<Estado> estadoCompByTopDeaths;

Notice that Total and Time are redundant sort orders unless for Entries unless estimates are ever revised down – and that time is probably preferred. Similarly TopTotal etc. is redundant for Estado.

By “numDeaths” in an Entry, do you mean in a day / unit of time, or do you mean the cumulative total deaths?

1 Like