Lasso Soft Inc. > Home

[macfindersort]

Linkmacfindersort
AuthorPier Kuipers
CategoryArray
Version8.5.x
LicensePublic Domain
Posted08 Sep 2009
Updated08 Sep 2009
More by this author...

Description

Promted by a thread on LassoTalk and inspired by Johan Sölve's alphasorter, I came up with this tag to sort strings in the same quirky way as the Macintosh Finder. Normal sorting of strings has this result:
File 12.pdf
File 3.pdf
File 6.pdf
The Mac Finder however, will honour the numerical values like so:
File 3.pdf
File 6.pdf
File 12.pdf
..And that is what this tag is trying to mimic.
http://www.listsearch.com/Lasso/Thread/index.lasso?22876#251316

Sample Usage

local('strings' = array(
	'File 6.pdf',
	'File 12.pdf',
	'File 3.pdf'
	)
);

#strings -> (sortwith: \macfindersort);

iterate(#strings,local('p'));
	#p + '
\n'; /iterate;

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag('macfindersort', 
			-description='sorts an array of strings in the same way as the Macintosh Finder, honouring numerical values',
			-required='left', 
			-copy, 
			-required='right', 
			-copy); 
    if((#left -> size > 1) && (#right -> size > 1)); 
		local('leftarray' = array);
		local('leftsplit' = (#left->split('')));
		local('previousleft'='');
		local('thisleft'='');
		iterate(#leftsplit, local('currentleft'));
			if((#currentleft->isdigit)&&(#previousleft->isdigit));
				#thisleft += string(#currentleft);
				#leftarray -> remove;	
			else;
				#thisleft = #currentleft;
			/if;
			if(#thisleft -> isdigit);
				#leftarray -> insert(integer(#thisleft));
			else;
				#leftarray -> insert(string(#thisleft));
			/if;
			#previousleft = #currentleft;
		/iterate;
		local('rightarray' = array);
		local('rightsplit' = (#right->split('')));
		local('previousright'='');
		local('thisright'='');
		iterate(#rightsplit, local('currentright'));
			if((#currentright->isdigit)&&(#previousright->isdigit));
				#thisright += string(#currentright);
				#rightarray -> remove;	
			else;
				#thisright = #currentright;
			/if;
			if(#thisright -> isdigit);
				#rightarray -> insert(integer(#thisright));
			else;
				#rightarray -> insert(string(#thisright));
			/if;
			#previousright = #currentright;
		/iterate;
    /if; 
    return: (#leftarray > #rightarray ? -1 | 0); 
/define_tag; 

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft