Page 1 of 1
fioDread in alphabetical order?
Posted: Sat Jan 24, 2009 2:44 am
by kouky
Hi, I am using the fioDread to scan and display the content of a folder from an USB drive.
But it looks like fioDread sort files by date, and I wish it could sort files by alphabetical order.
Is there a way to do it?
this is the code I'm using:
Code: Select all
while (fioDread(ret, &record) > ) { ... }
Posted: Sat Jan 24, 2009 7:55 am
by J.F.
Most directory return services return entries in the order they're found in the filesystem structure. Given that most file managers copy by date (when you copy the next file over, it's a later date - even if only by a few seconds), those entries are likely to be "sorted" by date when read back.
If you want entries sorted by something other than the order they exist in the directory, you generally have to sort the entries by hand after reading them. You'll see that in all sorts of apps. If you read the entries into a linked list, you could sort by name while reading the entries by inserting the node according to the name.
Posted: Sat Jan 24, 2009 8:30 am
by kouky
Oh my god!
That's gonna be tough job to write bymyself code to sort it by alphabetical order...
Thanks for your quick reply
Posted: Sat Jan 24, 2009 10:20 am
by J.F.
kouky wrote:Oh my god!
That's gonna be tough job to write bymyself code to sort it by alphabetical order...
Thanks for your quick reply
It's not THAT tough! Here's my sort routine used in Doom on the PSP after I load the entries from the current directory.
Code: Select all
// sort them!
for (i=0; i<maxfiles-1; i++)
{
char tempfilename[FILENAME_MAX];
char temppath[FILENAME_MAX];
int tempflags;
if ((!thefiles[i].flags && thefiles[i+1].flags) || // directories first
(thefiles[i].flags && thefiles[i+1].flags && strcasecmp(thefiles[i].filename, thefiles[i+1].filename) > 0) ||
(!thefiles[i].flags && !thefiles[i+1].flags && strcasecmp(thefiles[i].filename, thefiles[i+1].filename) > 0))
{
strcpy(tempfilename, thefiles[i].filename);
strcpy(temppath, thefiles[i].path);
tempflags = thefiles[i].flags;
strcpy(thefiles[i].filename, thefiles[i+1].filename);
strcpy(thefiles[i].path, thefiles[i+1].path);
thefiles[i].flags = thefiles[i+1].flags;
strcpy(thefiles[i+1].filename, tempfilename);
strcpy(thefiles[i+1].path, temppath);
thefiles[i+1].flags = tempflags;
i = -1;
}
}
It's pretty much just a really simple bubble sort. Not the fastest thing in the world, but you won't notice the time on anything short of several hundred files. :)
The next time I update this, I'll probably switch to a linked list instead of an array, and do the sort on the fly.