[ai] alphabetical order

This commit is contained in:
Stefan Ostermann 2025-09-22 22:07:43 +02:00
parent 22f8d3eec3
commit 6b942a8e07
1 changed files with 53 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#include "DirectoryNode.h" #include "DirectoryNode.h"
#include "globals.h" #include "globals.h"
#include <algorithm>
DirectoryNode::DirectoryNode(const String &nodeName) DirectoryNode::DirectoryNode(const String &nodeName)
: name(nodeName), currentPlaying(nullptr) : name(nodeName), currentPlaying(nullptr)
@ -97,10 +98,9 @@ void DirectoryNode::buildDirectoryTree(const char *currentPath)
mp3Files.clear(); mp3Files.clear();
ids.clear(); ids.clear();
// Reserve memory to reduce heap fragmentation (optimization 3) // First collect entries so we can sort them alphabetically
subdirectories.reserve(8); // Reserve space for 8 subdirectories std::vector<String> dirNames;
mp3Files.reserve(16); // Reserve space for 16 MP3 files std::vector<String> fileNames;
ids.reserve(16); // Reserve space for 16 IDs
File rootDir = SD.open(currentPath); File rootDir = SD.open(currentPath);
while (true) while (true)
@ -113,22 +113,61 @@ void DirectoryNode::buildDirectoryTree(const char *currentPath)
if (entry.isDirectory() && entry.name()[0] != '.' && strcmp(entry.name(), sys_dir.c_str())) if (entry.isDirectory() && entry.name()[0] != '.' && strcmp(entry.name(), sys_dir.c_str()))
{ {
DirectoryNode *newNode = new DirectoryNode(entry.name()); dirNames.push_back(String(entry.name()));
subdirectories.push_back(newNode);
newNode->buildDirectoryTree((String(currentPath) + entry.name()).c_str());
} }
else if (String(entry.name()).endsWith(".mp3") || String(entry.name()).endsWith(".MP3")) else
{ {
String fullPath = String(currentPath); String entryName = entry.name();
if (!fullPath.endsWith("/")) if (entryName.endsWith(".mp3") || entryName.endsWith(".MP3"))
fullPath += "/"; {
fullPath += entry.name(); fileNames.push_back(entryName);
mp3Files.push_back(fullPath); }
ids.push_back(getNextId());
} }
entry.close(); entry.close();
} }
rootDir.close(); rootDir.close();
// Case-insensitive alphabetical sort
auto ciLess = [](const String &a, const String &b) {
String al = a;
String bl = b;
al.toLowerCase();
bl.toLowerCase();
return al.compareTo(bl) < 0;
};
std::sort(dirNames.begin(), dirNames.end(), ciLess);
std::sort(fileNames.begin(), fileNames.end(), ciLess);
// Reserve memory to reduce heap fragmentation
subdirectories.reserve(dirNames.size());
mp3Files.reserve(fileNames.size());
ids.reserve(fileNames.size());
// Create subdirectories in alphabetical order
for (const String &dirName : dirNames)
{
DirectoryNode *newNode = new DirectoryNode(dirName);
subdirectories.push_back(newNode);
String childPath = String(currentPath);
if (!childPath.endsWith("/"))
childPath += "/";
childPath += dirName;
newNode->buildDirectoryTree(childPath.c_str());
}
// Add MP3 files in alphabetical order
for (const String &fileName : fileNames)
{
String fullPath = String(currentPath);
if (!fullPath.endsWith("/"))
fullPath += "/";
fullPath += fileName;
mp3Files.push_back(fullPath);
ids.push_back(getNextId());
}
} }
void DirectoryNode::printDirectoryTree(int level) const void DirectoryNode::printDirectoryTree(int level) const