fork(3) download
  1. import java.time.YearMonth;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.Locale;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9. public static void main(String args[]) {
  10. List<String> list = List.of("12-2014", "11-2012", "5-2014", "8-2012");
  11. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M-uuuu", Locale.ENGLISH);
  12. List<String> sorted =
  13. list.stream()
  14. .map(s -> YearMonth.parse(s, dtf))
  15. .sorted(Comparator.reverseOrder())
  16. .map(ym -> dtf.format(ym))
  17. .collect(Collectors.toList());
  18.  
  19. // Display the list
  20. System.out.println(sorted);
  21. }
  22. }
Success #stdin #stdout 0.11s 51300KB
stdin
Standard input is empty
stdout
[12-2014, 5-2014, 11-2012, 8-2012]