Fixes problems in sorting and generating the character indexes
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good

This commit is contained in:
2025-08-03 04:45:21 +02:00
parent f9674568ba
commit e5aaa29c66

View File

@@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
@@ -116,17 +115,13 @@ public final class BookFileHelper {
*/
@NotNull
public static Map<Character, Integer> populateLetterIndices(@NotNull List<String> books) {
List<Character> firstCharacter = new ArrayList<>(books.size());
for (String bookIdentifier : books) {
firstCharacter.add(BookFormatter.stripColor(bookIdentifier).toLowerCase().charAt(0));
}
Map<Character, Integer> firstEncounter = new HashMap<>();
for (int characterIndex = 0; characterIndex <= 25; characterIndex++) {
char character = (char) ('a' + characterIndex);
int index = Collections.binarySearch(firstCharacter, character);
if (index >= 0) {
firstEncounter.put(character, index);
Character current = null;
for (int i = 0; i < books.size(); i++) {
char first = BookFormatter.stripColor(books.get(i)).toLowerCase().charAt(0);
if (current == null || current != first) {
current = first;
firstEncounter.put(first, i);
}
}
return firstEncounter;
@@ -170,7 +165,8 @@ public final class BookFileHelper {
// Sort the book list
Comparator<String> bookComparator = Comparator.naturalOrder();
fileList.sort((a, b) -> bookComparator.compare(BookFormatter.stripColor(a), BookFormatter.stripColor(b)));
fileList.sort((a, b) -> bookComparator.compare(BookFormatter.stripColor(a).toLowerCase(),
BookFormatter.stripColor(b).toLowerCase()));
return fileList;
}