From 82a11efa31f0a0d259e089e702f58dd9c141ed02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Mon, 29 May 2023 21:51:07 -0600 Subject: New get function and sorting problems. Get function for filtered to dos. Sorting can now compare null objects. Reordered sorting because of a Concurrent Modification Exception. --- src/main/java/com/encora/Main.java | 7 ++----- src/main/java/com/encora/ToDosRepository.java | 12 ++++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java index 7793be9..ea3a2a4 100644 --- a/src/main/java/com/encora/Main.java +++ b/src/main/java/com/encora/Main.java @@ -6,10 +6,7 @@ import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Objects; +import java.util.*; @SpringBootApplication @RestController @@ -215,7 +212,7 @@ public class Main { } final int pageSize = 10; - List myToDos = toDosRepository.filteredToDos; + List myToDos = toDosRepository.getFilteredToDos(); int fromIndex = (page - 1) * pageSize; if (myToDos.size() <= fromIndex) { diff --git a/src/main/java/com/encora/ToDosRepository.java b/src/main/java/com/encora/ToDosRepository.java index a4c9a10..c4bdcba 100644 --- a/src/main/java/com/encora/ToDosRepository.java +++ b/src/main/java/com/encora/ToDosRepository.java @@ -171,7 +171,7 @@ public class ToDosRepository implements JpaRepository { break; case "DueDate": - comparator = Comparator.comparing(ToDos::getDueDate); + comparator = Comparator.comparing(ToDos::getDueDate, Comparator.nullsLast(Comparator.naturalOrder())); break; default: @@ -187,16 +187,20 @@ public class ToDosRepository implements JpaRepository { // Filter and then sort all of our to dos. public void refreshFilteredToDos(Sort sort, String name, String priority, String done) throws Exception { - this.filteredToDos = this.findAllWithFilter(name, priority, done); - try { Comparator comparator = this.getToDoComparator(sort); - Collections.sort(this.filteredToDos, comparator); + + this.filteredToDos = this.findAllWithFilter(name, priority, done); + this.filteredToDos.sort(comparator); } catch (Exception e) { throw new RuntimeException(e); } } + public List getFilteredToDos() { + return this.filteredToDos; + } + /* * N O T Y E T D E F I N E D . */ -- cgit v1.2.3