Buble Sort Arraylist - Java

Asked

Viewed 69 times

0

I need to sort an array based on the Date field of the same, for that I made this code.

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");           
for(int i = 0; i < arrayCapacitacaoAux.size(); i++){
    String dadosA = (String)arrayCapacitacaoAux.get(i);
    String infoA[] = dadosA.split("\\|");                   
    Date dataA = (Date)format.parse(infoA[9]);

    for(int j = 0; j < arrayCapacitacaoAux.size()-i; j++){
        String dadosB = (String)arrayCapacitacaoAux.get(j);
        String infoB[] = dadosB.split("\\|");   
        Date dataB = (Date)format.parse(infoB[9]);

        if (dataA.after(dataB)) {

        }

    }

}

There are some things I need to say first.

  1. I didn’t create the code at first, it’s an adjustment so I couldn’t interfere with the code, it follows sample of how the array is populated. Follow sample of how it’s done:

arrayCapacitacaoAux.add(pageContext.getAttribute("data").toString()+"|"+nomeCurso +"|"+local+"|"+imagemApresentacao+"|"+horario +"|"+cargaHoraria +"|"+publicoAlvo+"|"+paginaEdit+"|"+periodo+"|"+dataFinal);

And so I take the data the way it is in the code I posted.

  1. The idea is that the upper date (AFTER) was always above.

Anyway, I got to this part and kind of got lost, the only time I did Buble Sort was in C, but it doesn’t change so much, the problem is that now I’m using arraylist, can you give me a help on this ? Thank you guys!

1 answer

0

You can implement a Comparator with the sort rules you want and then use the method Collections.sort. Example:

List<String> arrayCapacitacaoAux = new ArrayList<>();
arrayCapacitacaoAux.add("data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|01/01/2016");
arrayCapacitacaoAux.add("data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|02/01/2016");
arrayCapacitacaoAux.add("data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|05/04/2016");
arrayCapacitacaoAux.add("data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|01/01/2017");

Collections.sort(arrayCapacitacaoAux, new Comparator<String>() {

    private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");

    @Override
    public final int compare(String o1, String o2) {
        LocalDate date1 = LocalDate.parse(o1.split("\\|")[9], dtf);
        LocalDate date2 = LocalDate.parse(o2.split("\\|")[9], dtf);
        return date2.compareTo(date1);
    }
});

arrayCapacitacaoAux.forEach(System.out::println);

Exit:

data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|01/01/2017
data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|05/04/2016
data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|02/01/2016
data|nomeDoCurso|local|imagemApresentacao|horario|cargaHoraria|publicoAlvo|paginaEdit|periodo|01/01/2016

Browser other questions tagged

You are not signed in. Login or sign up in order to post.