排序使用Comparator静态方法实现 (#812)

* 排序使用Comparator静态方法实现

* 修复排序逻辑错误

* fix compile error
pull/813/head
liaozan 2020-04-30 22:25:37 +08:00 committed by GitHub
parent 694667fcf5
commit 36b3a2d3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 24 deletions

View File

@ -38,6 +38,7 @@ import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -192,15 +193,8 @@ public class BackupServiceImpl implements BackupService {
return subPathStream
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_BACKUP_PREFIX))
.map(backupPath -> buildBackupDto(BACKUP_RESOURCE_BASE_URI, backupPath))
.sorted((leftBackup, rightBackup) -> {
// Sort the result
if (leftBackup.getUpdateTime() < rightBackup.getUpdateTime()) {
return 1;
} else if (leftBackup.getUpdateTime() > rightBackup.getUpdateTime()) {
return -1;
}
return 0;
}).collect(Collectors.toList());
.sorted(Comparator.comparingLong(BackupDTO::getUpdateTime).reversed())
.collect(Collectors.toList());
} catch (IOException e) {
throw new ServiceException("Failed to fetch backups", e);
}
@ -317,15 +311,8 @@ public class BackupServiceImpl implements BackupService {
return subPathStream
.filter(backupPath -> StringUtils.startsWithIgnoreCase(backupPath.getFileName().toString(), HaloConst.HALO_DATA_EXPORT_PREFIX))
.map(backupPath -> buildBackupDto(DATA_EXPORT_BASE_URI, backupPath))
.sorted((leftBackup, rightBackup) -> {
// Sort the result
if (leftBackup.getUpdateTime() < rightBackup.getUpdateTime()) {
return 1;
} else if (leftBackup.getUpdateTime() > rightBackup.getUpdateTime()) {
return -1;
}
return 0;
}).collect(Collectors.toList());
.sorted(Comparator.comparingLong(BackupDTO::getUpdateTime).reversed())
.collect(Collectors.toList());
} catch (IOException e) {
throw new ServiceException("Failed to fetch exported data", e);
}