Buble Sort ArrayList - Java

0

I need to sort an array based on the Date field of it, so 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 before.

  • I did not create the code at first, it's an adjustment so I could not interfere with the code, there follows a sample of how the array is filled. Here's a sample of how it's done:
  • arrayCapacitacaoAux.add(pageContext.getAttribute("data").toString()+"|"+nomeCurso +"|"+local+"|"+imagemApresentacao+"|"+horario +"|"+cargaHoraria +"|"+publicoAlvo+"|"+paginaEdit+"|"+periodo+"|"+dataFinal);

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

  • The idea is that the top date (AFTER) would always be up.
  • Finally, I arrived in this part and a half that I lost, the only time I did the Buble sort was in C, but it does not change so much, the problem is that I am now using arraylist, can you give me a help in this? Thank you guys!

        
    asked by anonymous 11.03.2017 / 17:34

    1 answer

    0

    You can implement a Comparator with the sort rules you want, and then use the Collections.sort method. 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);
    

    Output:

    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
    
        
    11.03.2017 / 19:08