daniel_silva Posted April 15, 2020 at 03:43 PM Report Share #617882 Posted April 15, 2020 at 03:43 PM 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 comment Share on other sites More sharing options...
Tim§id€ Posted April 15, 2020 at 03:48 PM Report Share #617883 Posted April 15, 2020 at 03:48 PM Boas, Podes usar Collections.sort ...º(>_<)º... Link to comment Share on other sites More sharing options...
daniel_silva Posted April 15, 2020 at 04:08 PM Author Report Share #617885 Posted April 15, 2020 at 04:08 PM Boas, Se não for pedir muito podia explicar melhor? É que sou novato nisto... Obrigado Link to comment Share on other sites More sharing options...
Tim§id€ Posted April 15, 2020 at 04:42 PM Report Share #617887 Posted April 15, 2020 at 04:42 PM 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 comment Share on other sites More sharing options...
daniel_silva Posted April 15, 2020 at 05:32 PM Author Report Share #617888 Posted April 15, 2020 at 05:32 PM Certo, tenho uma classe utentes e tenho as consultas e nas consultas tenho a data. Estou a usar java 8. Link to comment Share on other sites More sharing options...
Tim§id€ Posted April 15, 2020 at 07:11 PM Report Share #617889 Posted April 15, 2020 at 07:11 PM 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 comment Share on other sites More sharing options...
daniel_silva Posted April 17, 2020 at 01:34 AM Author Report Share #617900 Posted April 17, 2020 at 01:34 AM Boa noite, Muito obrigado pela ajuda. Link to comment Share on other sites More sharing options...
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