Fixes problems in sorting and generating the character indexes
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Books-Without-Borders/pipeline/head This commit looks good
This commit is contained in:
@@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -116,17 +115,13 @@ public final class BookFileHelper {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Map<Character, Integer> populateLetterIndices(@NotNull List<String> books) {
|
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<>();
|
Map<Character, Integer> firstEncounter = new HashMap<>();
|
||||||
for (int characterIndex = 0; characterIndex <= 25; characterIndex++) {
|
Character current = null;
|
||||||
char character = (char) ('a' + characterIndex);
|
for (int i = 0; i < books.size(); i++) {
|
||||||
int index = Collections.binarySearch(firstCharacter, character);
|
char first = BookFormatter.stripColor(books.get(i)).toLowerCase().charAt(0);
|
||||||
if (index >= 0) {
|
if (current == null || current != first) {
|
||||||
firstEncounter.put(character, index);
|
current = first;
|
||||||
|
firstEncounter.put(first, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return firstEncounter;
|
return firstEncounter;
|
||||||
@@ -170,7 +165,8 @@ public final class BookFileHelper {
|
|||||||
|
|
||||||
// Sort the book list
|
// Sort the book list
|
||||||
Comparator<String> bookComparator = Comparator.naturalOrder();
|
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;
|
return fileList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user