我正在尝试根据日期对
List<String>
进行排序。名单:
[
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0057c620ea6040849da9bceb977e7332-2023-09-11-19-30-10-00445.txt,
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_00e1cd4e425243bda8e09ee2b7b2334f-2023-09-11-19-30-02-00554.txt,
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_021b63466ff6406d8b30b25bda63b49f-2023-09-11-19-30-21-00218.txt,
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_049a681a00214155b7dab7c94c871341-2023-09-11-19-30-15-00036.txt,
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_068e490f3b78453293e84bdfb49112ad-2023-09-11-19-30-14-00288.txt,
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0694fca541d0486689c6591b16cb1fbc-2023-09-11-19-30-14-00405.txt,
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0a8d4975741e4b088ead2bfd1edee1ad-2023-09-11-19-30-07-00188.txt
]
我想要排序的部分是:
2023-09-11-19-30-10-00445
在 Java 中,您可以使用自定义
Comparator
根据每个文本的指定部分对字符串列表进行排序。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> fileList = new ArrayList<>();
fileList.add("datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0057c620ea6040849da9bceb977e7332-2023-09-11-19-30-10-00445.txt");
fileList.add("datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_00e1cd4e425243bda8e09ee2b7b2334f-2023-09-11-19-30-02-00554.txt");
// Add more entries to the fileList
sortFileListByDate(fileList);
for (String filePath : fileList) {
System.out.println(filePath);
}
}
static void sortFileListByDate(List<String> fileList) {
Collections.sort(fileList, new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
String[] tokens1 = str1.split("-");
String[] tokens2 = str2.split("-");
String dateStr1 = tokens1[tokens1.length - 7];
String dateStr2 = tokens2[tokens2.length - 7];
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSSSS");
try {
long date1 = dateFormat.parse(dateStr1).getTime();
long date2 = dateFormat.parse(dateStr2).getTime();
return Long.compare(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
});
}
}
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private static final String REGEX = "-(\\d{4}-\\d{2}-\\d{2}-\\d{2}-\\d{2}-\\d{2}-\\d{5})\\.txt$";
private static final Pattern PATTERN = Pattern.compile(REGEX);
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-SSSSS", Locale.ENGLISH);
private static int compare(String s1, String s2) {
Matcher m1 = PATTERN.matcher(s1);
m1.find();
LocalDateTime ts1 = LocalDateTime.parse(m1.group(1), FORMATTER);
Matcher m2 = PATTERN.matcher(s2);
m2.find();
LocalDateTime ts2 = LocalDateTime.parse(m2.group(1), FORMATTER);
return ts1.isAfter(ts2) ? 1 : ts1.isBefore(ts2) ? -1 : 0;
}
public static void main(String[] args) {
Comparator<String> c = Main::compare;
List<String> tmp = List.of("datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0057c620ea6040849da9bceb977e7332-2023-09-11-19-30-10-00445.txt",
"datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_00e1cd4e425243bda8e09ee2b7b2334f-2023-09-11-19-30-02-00554.txt",
"datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_021b63466ff6406d8b30b25bda63b49f-2023-09-11-19-30-21-00218.txt",
"datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_049a681a00214155b7dab7c94c871341-2023-09-11-19-30-15-00036.txt",
"datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_068e490f3b78453293e84bdfb49112ad-2023-09-11-19-30-14-00288.txt",
"datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0694fca541d0486689c6591b16cb1fbc-2023-09-11-19-30-14-00405.txt",
"datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0a8d4975741e4b088ead2bfd1edee1ad-2023-09-11-19-30-07-00188.txt");
List<String> list = new ArrayList<>(tmp);
Collections.sort(list, c);
list.forEach(System.out::println);
}
}
上面的代码实现了一个Comparator来对列表进行排序。
Comparator
使用 正则表达式 提取列表中每个字符串的相关部分。这是我运行上述代码时的输出:
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_00e1cd4e425243bda8e09ee2b7b2334f-2023-09-11-19-30-02-00554.txt
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0a8d4975741e4b088ead2bfd1edee1ad-2023-09-11-19-30-07-00188.txt
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0057c620ea6040849da9bceb977e7332-2023-09-11-19-30-10-00445.txt
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_068e490f3b78453293e84bdfb49112ad-2023-09-11-19-30-14-00288.txt
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_0694fca541d0486689c6591b16cb1fbc-2023-09-11-19-30-14-00405.txt
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_049a681a00214155b7dab7c94c871341-2023-09-11-19-30-15-00036.txt
datareplay/upstream/20230911/2023-09-11/WIRE_ISSN.WIRE_CRTD_021b63466ff6406d8b30b25bda63b49f-2023-09-11-19-30-21-00218.txt