SPUG: Interesting sort issue

Parr, Ryan Ryan.Parr at wwireless.com
Tue Oct 23 22:28:38 CDT 2001

I have spoken too soon. It seems that the sort routine was reverse
alphabetizing the directories, but properly alphabetizing files. This in
itself is weird, but was easily fixed:

if(-d $a && -d $b) {
	return $b_title cmp $a_title;
else {
	return $a_title cmp $b_title;

This code works, even when there's mixed directories and files. I do
still consider this strange. 

-- Ryan

-----Original Message-----
From: Parr, Ryan 
Sent: Tuesday, October 23, 2001 9:09 PM
To: spug-list at pm.org
Subject: Interesting sort issue

For the current implementation of my department's website, I've made a
module (FileAPI) that allows me to utilise pages as data sources easily.
I've also got an SSI CGI script that dynamically builds the drop-down menu
and directory listings according to the directory structure which is
determined using File::Find. The problem I'm running into is that sorting is
very quirky. 

The following code:
sub preprocess {	# File::Find passes the directory listing to
&preprocess if defined 
			# before doing anything with it, specifically for
sorting or excluding 
			# directory entries
	return sort sorter @_;

sub sorter {
	my $a_title = lc(FileAPI->new($a)->getTitle());
	my $b_title = lc(FileAPI->new($b)->getTitle());

	$a_title cmp $b_title;
does alternating alphabetizing. For instance:

C Directory
	A Sub-Directory
	B Sub-Directory
	C Sub-Directory

B Directory
	A Sub-Directory
	B Sub-Directory
	C Sub-Directory

A Directory
	A Sub-Directory
	B Sub-Directory
	C Sub-Directory

and so on...

I consider this behavior to be bizarre. Any thoughts?

-- Ryan Parr

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/

More information about the spug-list mailing list