Хорошо, я закончил написание метода для сортировки списка здесь, это
ListIterator<CalibrationHistoryEntity> listIterator = allCalibrationList.listIterator();
while (listIterator.hasNext()) {
CalibrationHistoryEntity calibrationHistoryEntity = listIterator.next();
//Get all the index
List<Integer> indexes = findAllByName(allCalibrationList, calibrationHistoryEntity.getCalibrationName());
if (indexes.size() > 1) {
List<CalibrationHistoryEntity> calibrationHistoryEntities = new ArrayList<>();
for (int index : indexes) {
calibrationHistoryEntities.add(allCalibrationList.get(index));
}
//Sort by biggest time
calibrationHistoryEntities.sort(Comparator.comparing(CalibrationHistoryEntity::getCalibrationDate).reversed());
for (int i = 1; i < calibrationHistoryEntities.size(); i++) {
//Remove others
listIterator.remove();
}
}
}
и способ получения всего индекса
/**
* gets all the index via the name
*/
public static List<Integer> findAllByName(List<CalibrationHistoryEntity> allCalibrationList, String name) {
List<Integer> indexes = new ArrayList<>();
for (CalibrationHistoryEntity calibrationHistoryEntity : allCalibrationList) {
if (calibrationHistoryEntity.getCalibrationName().equals(name)) {
indexes.add(allCalibrationList.indexOf(calibrationHistoryEntity));
}
}
return indexes;
}
и я сделал ввод через
String[] names = {"UNIFORMITY", "Roi", "UNIFORMITY"};
SimpleDateFormat dt = new SimpleDateFormat("MMM dd hh:mm:ss zzz yyyy");
Date[] dates = new Date[0];
try {
dates = new Date[]{dt.parse("Oct 31 02:48:32 GMT+00:00 2018"),
dt.parse("Oct 31 02:48:25 GMT+00:00 2018"),
dt.parse("Oct 31 02:48:37 GMT+00:00 2018")};
} catch (ParseException e) {
e.printStackTrace();
}
List<CalibrationHistoryEntity> allCalibrationList = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
String name = names[i];
Date date = dates[i];
CalibrationHistoryEntity calibrationHistoryEntity = new CalibrationHistoryEntity();
calibrationHistoryEntity.setCalibrationName(name);
calibrationHistoryEntity.setCalibrationDate(date);
allCalibrationList.add(calibrationHistoryEntity);
}
Это вывод
Name: Roi Time:Wed Oct 31 05:48:25 GMT+03:00 2018
Name: UNIFORMITY Time:Wed Oct 31 05:48:37 GMT+03:00 2018