Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Characters

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0

I have a string ex."rRaAtT" or similar and I want to make it "AaRrTt", sorted in alphabetical order where an uppercase letter goes in front of a lower case letter.

Please help!!!
 
<?
function cmp ($a, $b) {
if ($a == $b) return 0;
// $a is upper and $b in lower (same letter)
if ($a == strtoupper($b)) return 1;
// $a is lower and $b in upper (same letter)
if ($b == strtoupper($a)) return -1;
// other cases
if (strtoupper($a) > strtoupper($b)) return -1;
return 1;
}

$a =&quot;rRaAtT&quot;;
echo &quot;OLD: $a<br>&quot;;

for($i=0 ; $i<strlen($a)-1 ; $i++){
for ($j=$i+1 ; $j<strlen($a);$j++){
if (cmp($a[$i],$a[$j])<0){
$tmp=$a[$i];
$a[$i]=$a[$j];
$a[$j]=$tmp;
}
}
}

echo &quot;NEW: $a<br>&quot;;
?> Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top