I want to display a List of objects sorted by a date argument in crescent order. I have tried many ways. But it doesn't work. It's not changing the order of the objects in the list. I visited this for help: Android: How to sort list items according to dates
How to Sort Date in descending order From Arraylist Date in android?
https://www.quora.com/How-do-I-sort-an-ArrayList-of-objects-by-each-objects-integer-parameter
Here is my code:
my Class Object
public class Evento extends GregorianCalendar {
public Calendar date;
public String tipo = " ";
public String a = " ";
public String b = " ";
public String c = " ";
And my MainActivity:
public void implementList (View view){
List <Evento> listEventosAll = new ArrayList<>();
listEventosAll.addAll(listEventos1);
listEventosAll.addAll(listEventos2);
Collections.sort(listEventosAll, new Comparator<Evento> (){
public int compare(Evento evento1, Evento evento2) {
if (evento1.date.getTime() == null || evento2.date.getTime() == null)
return 0;
return evento1.date.getTime().compareTo(evento2.date.getTime());
}
});
EventoAdapter adapter = new EventoAdapter(this,listEventosAll);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
populating ListEventos1
public static Evento setEvento(Calendar day, String a, String b) {
Evento evento = new Evento();
evento.date = day;
evento.a = a;
evento.b = b;
return evento;
}
// Creating the list:
public static ArrayList listEventos1(int year){
Calendar day = calculateCertainTuesday(year);
ArrayList <Evento> list = new ArrayList<Evento>();
list.add(setEvento(day, "Certain Tuesday”, "do this"));
day.add(Calendar.DAY_OF_MONTH, 1);
list.add(setEvento(day, "Certain Wendsday”, "do that"));
day.add(Calendar.DAY_OF_MONTH,-2);
list.add(setEvento(day, "Certain Monday", "do what"));
day.add(Calendar.DAY_OF_MONTH, -1);
list.add(setEvento(day, "SUNDAY", "don’t do"));
return list;
}