@Test public void sortsDescendingByDateAndInteger() { final String[] FILENAMES = new String[] { "/var/logs/my-app/2018-10-31/9.log", "/var/logs/my-app/2019-01-01/1.log", "/var/logs/my-app/1999-03-17/3.log", "/var/logs/my-app/2019-01-01/11.log", "/var/logs/my-app/2019-01-01/2.log", "/var/logs/my-app/2016-12-25/10.log", }; final String[] EXPECTED_RESULT = new String[] { "/var/logs/my-app/2019-01-01/11.log", "/var/logs/my-app/2019-01-01/2.log", "/var/logs/my-app/2019-01-01/1.log", "/var/logs/my-app/2018-10-31/9.log", "/var/logs/my-app/2016-12-25/10.log", "/var/logs/my-app/1999-03-17/3.log", }; assertThat(sort("/var/logs/my-app/%d{yyyy-MM-dd}/%i.log", FILENAMES), contains(EXPECTED_RESULT)); }
@SuppressWarnings("unchecked") void sort(String[] filenames) { Arrays.sort(filenames, new Comparator<String>() { @Override public int compare(String f1, String f2) { int result = 0; for (FilenameParser p : parsers) { Comparable c2 = p.parseFilename(f2); Comparable c1 = p.parseFilename(f1); if (c2 != null && c1 != null) { result += c2.compareTo(c1); } } if (result == 0) { result = f2.compareTo(f1); } return result; } }); }
FileSorter { @SuppressWarnings("unchecked") void sort(String[] filenames) { Arrays.sort(filenames, new Comparator<String>() { @Override public int compare(String f1, String f2) { int result = 0; for (FilenameParser p : parsers) { Comparable c2 = p.parseFilename(f2); Comparable c1 = p.parseFilename(f1); if (c2 != null && c1 != null) { result += c2.compareTo(c1); } } if (result == 0) { result = f2.compareTo(f1); } return result; } }); } }
FileSorter { @SuppressWarnings("unchecked") void sort(String[] filenames) { Arrays.sort(filenames, new Comparator<String>() { @Override public int compare(String f1, String f2) { int result = 0; for (FilenameParser p : parsers) { Comparable c2 = p.parseFilename(f2); Comparable c1 = p.parseFilename(f1); if (c2 != null && c1 != null) { result += c2.compareTo(c1); } } if (result == 0) { result = f2.compareTo(f1); } return result; } }); } FileSorter(FilenameParser ...parsers); }
FileSorter { @SuppressWarnings("unchecked") void sort(String[] filenames) { Arrays.sort(filenames, new Comparator<String>() { @Override public int compare(String f1, String f2) { int result = 0; for (FilenameParser p : parsers) { Comparable c2 = p.parseFilename(f2); Comparable c1 = p.parseFilename(f1); if (c2 != null && c1 != null) { result += c2.compareTo(c1); } } if (result == 0) { result = f2.compareTo(f1); } return result; } }); } FileSorter(FilenameParser ...parsers);  }
FileSorter { @SuppressWarnings("unchecked") void sort(String[] filenames) { Arrays.sort(filenames, new Comparator<String>() { @Override public int compare(String f1, String f2) { int result = 0; for (FilenameParser p : parsers) { Comparable c2 = p.parseFilename(f2); Comparable c1 = p.parseFilename(f1); if (c2 != null && c1 != null) { result += c2.compareTo(c1); } } if (result == 0) { result = f2.compareTo(f1); } return result; } }); } FileSorter(FilenameParser ...parsers);   }