daniel_silva 0 Posted April 15, 2020 Report Share Posted April 15, 2020 Boa tarde, Estou a fazer um projecto em que regista consultas de utentes e a minha dúvida é, como posso ordenar essa lista pela data de consultas? Estou a usar um arraylist para guardar os registos. Obrigado. Link to post Share on other sites
Tim§id€ 5 Posted April 15, 2020 Report Share Posted April 15, 2020 Boas, Podes usar Collections.sort ...º(>_<)º... Link to post Share on other sites
daniel_silva 0 Posted April 15, 2020 Author Report Share Posted April 15, 2020 Boas, Se não for pedir muito podia explicar melhor? É que sou novato nisto... Obrigado 😄 Link to post Share on other sites
Tim§id€ 5 Posted April 15, 2020 Report Share Posted April 15, 2020 No teu projeto tens: Um classe Utentes? nesta class por sua vez tens um argumento data consulta? estas a usar java 8+ ou inferior? ...º(>_<)º... Link to post Share on other sites
daniel_silva 0 Posted April 15, 2020 Author Report Share Posted April 15, 2020 Certo, tenho uma classe utentes e tenho as consultas e nas consultas tenho a data. Estou a usar java 8. Link to post Share on other sites
Tim§id€ 5 Posted April 15, 2020 Report Share Posted April 15, 2020 ArrayList tem um sort, podes usa-lo desta forma: List<Consulta> list = new ArrayList<>(); Consulta consulta1 = new Consulta("11/01/2000"); Consulta consulta2 = new Consulta("11/01/1990"); list.add(consulta1); list.add(consulta2); List<Consulta> list2 = list.stream().sorted(Comparator.comparing(Consulta::getDate)).collect(Collectors.toList()); for(Consulta consulta : list){ System.out.println(consulta.getDate()); } Nota que a única coisa que eu fiz foi no construtor recebo uma String e com SimpleDateFormat "dd/MM/yyyy" crio uma data. ...º(>_<)º... Link to post Share on other sites
daniel_silva 0 Posted April 17, 2020 Author Report Share Posted April 17, 2020 Boa noite, Muito obrigado pela ajuda 😁 Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now