Skip to content
Snippets Groups Projects
Commit 977ef553 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#564 - sort categories naturally

parent 86ecf9ab
No related branches found
No related tags found
No related merge requests found
...@@ -202,6 +202,11 @@ ...@@ -202,6 +202,11 @@
<artifactId>org.eclipse.jgit</artifactId> <artifactId>org.eclipse.jgit</artifactId>
<version>${jgit.version}</version> <version>${jgit.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.padler</groupId>
<artifactId>natorder</artifactId>
<version>1.0.1</version>
</dependency>
<!-- selenium --> <!-- selenium -->
<dependency> <dependency>
......
...@@ -4,11 +4,13 @@ import de.deadlocker8.budgetmaster.services.Resetable; ...@@ -4,11 +4,13 @@ import de.deadlocker8.budgetmaster.services.Resetable;
import de.deadlocker8.budgetmaster.transactions.Transaction; import de.deadlocker8.budgetmaster.transactions.Transaction;
import de.deadlocker8.budgetmaster.utils.Strings; import de.deadlocker8.budgetmaster.utils.Strings;
import de.thecodelabs.utils.util.Localization; import de.thecodelabs.utils.util.Localization;
import org.padler.natorder.NaturalOrderComparator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -101,16 +103,16 @@ public class CategoryService implements Resetable ...@@ -101,16 +103,16 @@ public class CategoryService implements Resetable
public List<Category> getAllCategories() public List<Category> getAllCategories()
{ {
localizeDefaultCategories(); localizeDefaultCategories();
return categoryRepository.findAllByOrderByNameAsc().stream() final List<Category> categories = categoryRepository.findAllByOrderByNameAsc();
.sorted(Comparator.comparing(c -> c.getName().toLowerCase())) categories.sort((c1, c2) -> new NaturalOrderComparator().compare(c1.getName(), c2.getName()));
.collect(Collectors.toList()); return categories;
} }
public List<Category> getAllCustomCategories() public List<Category> getAllCustomCategories()
{ {
return categoryRepository.findAllByTypeOrderByNameAsc(CategoryType.CUSTOM).stream() final List<Category> categories = categoryRepository.findAllByTypeOrderByNameAsc(CategoryType.CUSTOM);
.sorted(Comparator.comparing(c -> c.getName().toLowerCase())) categories.sort((c1, c2) -> new NaturalOrderComparator().compare(c1.getName(), c2.getName()));
.collect(Collectors.toList()); return categories;
} }
public void localizeDefaultCategories() public void localizeDefaultCategories()
......
...@@ -44,8 +44,17 @@ public class CategoryServiceTest ...@@ -44,8 +44,17 @@ public class CategoryServiceTest
Category category_AA = new Category("AA", "#ff0000", CategoryType.CUSTOM); Category category_AA = new Category("AA", "#ff0000", CategoryType.CUSTOM);
categories.add(category_AA); categories.add(category_AA);
Category category_0 = new Category("0", "#ff0000", CategoryType.CUSTOM); Category category_1 = new Category("1", "#ff0000", CategoryType.CUSTOM);
categories.add(category_0); categories.add(category_1);
Category category_11 = new Category("11", "#ff0000", CategoryType.CUSTOM);
categories.add(category_11);
Category category_2 = new Category("2", "#ff0000", CategoryType.CUSTOM);
categories.add(category_2);
Category category_AABB = new Category("AABB", "#ff0000", CategoryType.CUSTOM);
categories.add(category_AABB);
Category category_aa = new Category("aa", "#ff0000", CategoryType.CUSTOM); Category category_aa = new Category("aa", "#ff0000", CategoryType.CUSTOM);
categories.add(category_aa); categories.add(category_aa);
...@@ -54,8 +63,11 @@ public class CategoryServiceTest ...@@ -54,8 +63,11 @@ public class CategoryServiceTest
Mockito.when(categoryRepository.findByType(CategoryType.REST)).thenReturn(CATEGORY_REST); Mockito.when(categoryRepository.findByType(CategoryType.REST)).thenReturn(CATEGORY_REST);
Mockito.when(categoryRepository.findAllByOrderByNameAsc()).thenReturn(categories); Mockito.when(categoryRepository.findAllByOrderByNameAsc()).thenReturn(categories);
assertThat(categoryService.getAllCategories()).hasSize(6) System.out.println(List.of(category_1, category_2, category_11, category_AA, category_aa, category_AABB, category_BB, CATEGORY_NONE, CATEGORY_REST));
.containsExactly(category_0, category_AA, category_aa, category_BB, CATEGORY_NONE, CATEGORY_REST); System.out.println(categoryService.getAllCategories());
assertThat(categoryService.getAllCategories()).hasSize(9)
.containsExactly(category_1, category_2, category_11, category_AA, category_aa, category_AABB, category_BB, CATEGORY_NONE, CATEGORY_REST);
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment