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 &#40;i=0; i<maxfiles-1; i++&#41;
	&#123;
		char tempfilename&#91;FILENAME_MAX&#93;;
		char temppath&#91;FILENAME_MAX&#93;;
		int tempflags;

		if &#40;&#40;!thefiles&#91;i&#93;.flags && thefiles&#91;i+1&#93;.flags&#41; || // directories first
			&#40;thefiles&#91;i&#93;.flags && thefiles&#91;i+1&#93;.flags && strcasecmp&#40;thefiles&#91;i&#93;.filename, thefiles&#91;i+1&#93;.filename&#41; > 0&#41; ||
			&#40;!thefiles&#91;i&#93;.flags && !thefiles&#91;i+1&#93;.flags && strcasecmp&#40;thefiles&#91;i&#93;.filename, thefiles&#91;i+1&#93;.filename&#41; > 0&#41;&#41;
		&#123;
			strcpy&#40;tempfilename, thefiles&#91;i&#93;.filename&#41;;
			strcpy&#40;temppath, thefiles&#91;i&#93;.path&#41;;
			tempflags = thefiles&#91;i&#93;.flags;
			strcpy&#40;thefiles&#91;i&#93;.filename, thefiles&#91;i+1&#93;.filename&#41;;
			strcpy&#40;thefiles&#91;i&#93;.path, thefiles&#91;i+1&#93;.path&#41;;
			thefiles&#91;i&#93;.flags = thefiles&#91;i+1&#93;.flags;
			strcpy&#40;thefiles&#91;i+1&#93;.filename, tempfilename&#41;;
			strcpy&#40;thefiles&#91;i+1&#93;.path, temppath&#41;;
			thefiles&#91;i+1&#93;.flags = tempflags;
			i = -1;
		&#125;
	&#125;
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.