Home > Article > Backend Development > Sort string PHP
The sort string is useful for an organized string with the required manner in the PHP language. The sort string is a string method to sorting the given string to the required format using PHP language. The sort string is arranging the given sort string as per sorting functions in the PHP technology. The sort string is categorizing and assembling the available string as per the web application’s requirements. The sort string is settling the string as per required ascending or descending order in the PHP coding language.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
There are many ways to sorting the string. These methods of sort string are below.
String Convert into an array and use sort () method.
$sortstring = 'sadycetfimlog';
$stringndarray = str_split($sortstring);
sort($stringndarray);
rsort($stringndarray);
$stringndarray = implode($stringndarray);
echo $stringndarray;
Example: the sort string with ascending or descending order example and output.
<!DOCTYPE html> <html> <body> <h3> Ascending Order of the Sort String </h3> <?php $sortstring = "sadycetfimlogb"; echo "given string is : <b>$sortstring </b><br/> "; $stringndarray = str_split($sortstring); sort($stringndarray); $stringndarray = implode($stringndarray); echo " sorting string in the ascending order: <b>$stringndarray</b><br/>"; ?> <h3> Descending Order of the Sort String </h3> <?php $sortstring = "bnhrzsadycetfimlog"; echo "given string is : <b>$sortstring </b><br/> "; $stringndarray = str_split($sortstring); rsort($stringndarray); $stringndarray = implode($stringndarray); echo " sorting string in the descending order: <b>$stringndarray</b>"; ?> </body> </html>
Output:
String swap the position and use arguments to sorting the string.
function sortStringphp (place arguments here…) {write code here…}
$sortstring = 'jhjabcdewyxdef';
$stringlength;
$currentposition;
function sortStringphp(&$sortstring, $stringlength, $currentposition=0) { write code here… }
if($currentposition == $stringlength){ return; }
$nextposition = $currentposition + 1;
while($nextposition< $stringlength){ if($sortstring[$nextposition] < $sortstring[$currentposition]){ $tempstring = $sortstring[$nextposition]; $sortstring[$nextposition] = $sortstring[$currentposition]; $sortstring[$currentposition] = $tempstring; } $nextposition++; }
sortStringphp($sortstring, $stringlength, $currentposition+1);
sortStringphp($sortstring,strlen($sortstring)); echo $sortstring;
Example:
<!DOCTYPE html> <html> <body> <h3> Ascending Order </h3> <?php $sortstring = 'iamgoodinthisplace'; echo "the given string : <b> $sortstring </b> <br/>"; $stringlength; $currentposition; function sortStringphp(&$sortstring, $stringlength, $currentposition=0) { $nextposition = $currentposition + 1; while($nextposition < $stringlength){ if($sortstring[$nextposition] < $sortstring[$currentposition]){ $tempstring = $sortstring[$nextposition]; $sortstring[$nextposition] = $sortstring[$currentposition]; $sortstring[$currentposition] = $tempstring; } $nextposition++; } if($currentposition == $stringlength){ return; } sortStringphp($sortstring, $stringlength, $currentposition+1); } sortStringphp($sortstring,strlen($sortstring)); echo " the sorted string : <b> $sortstring </b>"; ?> <h3> Descending Order </h3> <?php $sortstring1 = 'iamgoodinthisplace'; echo "the given string : <b> $sortstring1 </b> <br/>"; $stringlength1; $currentposition1; function sortStringphp1(&$sortstring1, $stringlength1, $currentposition1=0) { if($currentposition1 == $stringlength1) return $nextposition1 = $currentposition1 + 1; while($nextposition1 < $stringlength1){ if($sortstring1[$nextposition1] < $sortstring1[$currentposition1]){ $tempstring1 = $sortstring1[$nextposition1]; $sortstring1[$nextposition1] = $sortstring1[$currentposition1]; $sortstring1[$currentposition1] = $tempstring1; } $nextposition1++; } sortStringphp1($sortstring1, $stringlength1, $currentposition1+1); } sortStringphp1($sortstring1,strlen($sortstring1)); echo " the sorted string : <b> $sortstring1 </b>"; ?> </body> </html>
Output:
The Quicksort algorithm uses to sort strings.
$stringleft = $stringright = '';
$stringlength = strlen($sortstring)-1 ;
if ($stringlength <= 0) { return $sortstring; }
$pivot = floor($stringlength/2);
do{ write sort string code here.. }while(sort string condition…)
if ($stringlength == $middlestring){ continue; }
if ($sortstring[$stringlength] >= $sortstring[$middlestring]) { $stringleft = $stringleft.$sortstring[$stringlength]; } else { $stringright = $stringright.$sortstring[$stringlength]; }
return sortStringphp($stringleft).$sortstring[$middlestring].sortStringphp($stringright);
$givenstring = sortStringphp ("goodtohaveacoffee"); echo " the sort string : <b>$givenstring</b>"
Example:
Ascending Order
$givenstring"; ?>Descending Order
$givenstring"; ?>
Output:
The above is the detailed content of Sort string PHP. For more information, please follow other related articles on the PHP Chinese website!