VB icon

Ordering Words Alphabetically

Email
Submitted on: 1/1/2015 3:05:00 PM
By: Jo B (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 1261
 
     This code shows how we can alphabetically order an array of words. I think it's pretty easy to understand. I also attached a 'counter' so we can track how many msecs the procedure will take. I added a function that will 'transform' all string to lower case as well as removing accented characters. This way we're sure the order will be correct.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Ordering Words Alphabetically
// Description:This code shows how we can alphabetically order an array of words. I think it's pretty easy to understand. I also attached a 'counter' so we can track how many msecs the procedure will take. I added a function that will 'transform' all string to lower case as well as removing accented characters. This way we're sure the order will be correct.
// By: Jo B (from psc cd)
//**************************************

<?php
function chgString($string) { 
return strtolower(strtr($string, 
"", 
"SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy")); 
}
//if $order != 'ASC' result will be 'DESC'
 $order="ASC";
//You can add as many as you want
 $str[0]="john";
 $str[1]="bettencourt";
 $str[2]="francis";
 $str[3]="susan";
 $str[4]="mary";
 $total=count($str);
 $a=1;
 while($a<$total+1){
 	$i=0;
 	while($i<$total+1){
 		if (strcmp(chgString($flag),chgString($str[$i]))<0){
 		$flagid=$i;
 		$flag=$str[$i];
 		}
 	$i++;
 	}
 if ($order=="ASC"){
 $strfinal = $str[$flagid]."<BR>".$strfinal;
 }else{
 $strfinal = $strfinal."<BR>".$str[$flagid];
 }
 $str[$flagid]="";
 $flag="";
 $a++;
 }
 //the result is helded in var $strfinal
 $timeEnd=microtime();
 $deltatime=($timeEnd-$timeStart);
 $esttime=($deltatime*10000)/$total;
 echo "<B>Alphabetical Result:</B><BR>$strfinal</B><BR>Done in $deltatime msecs";
 echo "<BR>The estimated time for performing this operation on 10000 words is aprox: $esttime ms (can vary depending on the words size)";
 ?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.