Sort Collection

0

I have a class called Object and inside it has a Collection of Events , summarizing it like this:

public class Objeto{

private Collection<Evento> eventos;
}

In the Event class, I have my attributes and I have one of them:

@Getter
    @Setter
    @Column
    @Temporal(TemporalType.TIMESTAMP)
    private Date horario;

On my screen I'm going to get a% object_with%. I need the following:

If in my Event class that is inside Object I have the Collection field. I need my% object% to be sorted in descending order to horario . Ex:

10/12/2015 10:00
10/12/2015 09:00
...

I tried this way but without success:

@SuppressWarnings("deprecation")
    public Collection<Objeto> ordenarLista() {
        Collection<Objeto> objetos = service.findAll();
        Collection<Objeto> lista = new ArrayList<Objeto>();
        Date referencia = new Date("10/01/2000 00:00:00");
        for (Objeto objeto : objetos) {
            for (Evento evento : objeto.getEventos()){
                if (evento.getHorario().after(referencia)){
                    lista.add(objeto);
                    referencia = evento.getHorario();
                }
            }
        }
        return lista;
    }

Does anyone know how I can resolve?

    
asked by anonymous 27.03.2017 / 18:51

2 answers

1

If you are using Java 8, you can do the following:

List<Evento> lista = new ArrayList<>(service.findAll());
lista.sort(new Comparator<Evento>() {
    @Override
    public int compare(Evento o1, Evento o2) {
        return o1.getHorario().compareTo(o2.getHorario());
    }
});
return lista;

If you are using Java 7 or earlier, there is also Collections.sort :

List<Evento> lista = new ArrayList<>(service.findAll());
Collections.sort(lista, new Comparator<Evento>() {
    @Override
    public int compare(Evento o1, Evento o2) {
        return o1.getHorario().compareTo(o2.getHorario());
    }
});
return lista;

Note that the example is not null-safe , but this is a quick fix normally

EDIT

Douglas noticed that my suggestions have probelmas have problems. Considering that within% with% I have a Objeto of List , I want the Evento that has the latest event to appear in the freight:

List<Objecto> objetos = new ArrayList<>(service.findAll());
// ordenar todos os eventos dentro de cada objeto
for (Objeto obj: objetos) {
    obj.getEventos().sort(new Comparator<Evento>() {
        @Override
        public int compare(Evento o1, Evento o2) {
            // invertendo a ordem, primeiro os eventos maiores
            return -o1.getHorario().compareTo(o2.getHorario());
        }
    });
}
// ordenar usando como base o maior dos eventos
objetos.sort(new Comparator<Objeto>() {
        @Override
        public int compare(Objeto o1, Objeto o2) {
            // levando em comparação os maiores eventos que, devido à ordenação anterior, estão em obj.getEventos().get(0)
            return -o1.getEventos().get(0).getHorario().compareTo(o2.getEventos().get(0).getHorario());
        }
    });
    
27.03.2017 / 20:11
0

Use% 8 of Java 8, such as method references , to simplify the work, as follows:

List<Evento> eventos = objeto.getEventos();
eventos.sort(Comparator.comparing(Evento::getHorario))

In this case the event list will be sorted in ascending order of Time.

As you want, use features :

List<Evento> eventos = objeto.getEventos();
eventos.sort(Comparator.comparing(Evento::getHorario).reversed())
    
27.03.2017 / 19:51