Updated April 10, 2023
Introduction to 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.
How to sort strings in PHP using various ways?
There are many ways to sorting the string. These methods of sort string are below.
1. The first method of the Sort String
String Convert into an array and use sort () method.
- Initialize the string with the required value on the coding page.
$sortstring = 'sadycetfimlog';
- Convert the string into an array using the string split method.
$stringndarray = str_split($sortstring);
- Use the sort string method as per the user’s requirement.
- Use the sort() method to sort the string as per Ascending order.
sort($stringndarray);
- Use the rsort() method to sort the string as per descending order.
rsort($stringndarray);
- Convert an array into the string using the implode method.
$stringndarray = implode($stringndarray);
- Return the sorting string in the PHP language.
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:
2. The second method of the Sort String
String swap the position and use arguments to sorting the string.
- The sort string creates a function.
function sortStringphp (place arguments here…) {write code here…}
- Create a string variable to sort the string.
$sortstring = 'jhjabcdewyxdef';
- Create a second-string variable to know the length of the given string.
$stringlength;
- Create a third-string variable to know the position of the string element and initialize with zero.
$currentposition;
- Place these string variables in the function as arguments.
function sortStringphp(&$sortstring, $stringlength, $currentposition=0) {
write code here…
}
- If the string current position and length of the string are equal then return the string.
if($currentposition == $stringlength){ return; }
- Create the one variable to increments the character of the string and initialize it.
$nextposition = $currentposition + 1;
- Use the algorithm of Swapping the position of the string characters.
while($nextposition< $stringlength){
if($sortstring[$nextposition] < $sortstring[$currentposition]){
$tempstring = $sortstring[$nextposition];
$sortstring[$nextposition] = $sortstring[$currentposition];
$sortstring[$currentposition] = $tempstring;
}
$nextposition++;
}
- Use the recursive function to avoid recursion inside of the main function.
sortStringphp($sortstring, $stringlength, $currentposition+1);
- Return the sort string in the PHP.
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:
3. The third method of the Sort String
The Quicksort algorithm uses to sort strings.
- The two variables create to help to make a partition of the string.
$stringleft = $stringright = '';
- The variable creates to measure the length of the string.
$stringlength = strlen($sortstring)-1 ;
- If the length of the string is equal to zero then return the string and stop sorting.
if ($stringlength <= 0) {
return $sortstring;
}
- Make a variable to create the middle of the string (pivot).
$pivot = floor($stringlength/2);
- Use the do-while loop for the sorting algorithm.
do{
write sort string code here..
}while(sort string condition…)
- If length of the string and middle string variable is equal then stop the break and continue the next loop.
if ($stringlength == $middlestring){
continue;
}
- Use ascending or descending sorting logic as per requirement.
if ($sortstring[$stringlength] >= $sortstring[$middlestring]) {
$stringleft = $stringleft.$sortstring[$stringlength];
} else {
$stringright = $stringright.$sortstring[$stringlength];
}
- Return the sorting string according to the algorithm.
return sortStringphp($stringleft).$sortstring[$middlestring].sortStringphp($stringright);
- print the sort string in the php.
$givenstring = sortStringphp ("goodtohaveacoffee");
echo " the sort string : <b>$givenstring</b>"
Example:
<html>
<body>
<?php
$stringsort = 'goodtohaveacoffee';
echo "given string : $stringsort";
?>
<h3> Ascending Order </h3>
<?php
function sortStringphp($sortstring) {
$stringleft = $stringright = '';
$stringlength = strlen($sortstring)-1 ;
if ($stringlength <= 0) {
return $sortstring;
}
$middlestring = floor($stringlength/2);
do {
if ($stringlength == $middlestring){
continue;
}
if ($sortstring[$stringlength] <= $sortstring[$middlestring]) {
$stringleft = $stringleft.$sortstring[$stringlength];
} else {
$stringright = $stringright.$sortstring[$stringlength];
}
} while ($stringlength--);
return sortStringphp($stringleft).$sortstring[$middlestring].sortStringphp($stringright);
}
$givenstring =
sortStringphp ("goodtohaveacoffee");
echo " the sort string : <b>$givenstring</b>";
?>
<h3> Descending Order </h3>
<?php
function sortStringphp1($sortstring) {
$stringleft = $stringright = '';
$stringlength = strlen($sortstring)-1 ;
if ($stringlength <= 0){
return $sortstring;
}
$middlestring = floor($stringlength/2);
do {
if ($stringlength == $middlestring){
continue;
}
if ($sortstring[$stringlength] >= $sortstring[$middlestring]) {
$stringleft = $stringleft.$sortstring[$stringlength];
} else {
$stringright = $stringright.$sortstring[$stringlength];
}
} while ($stringlength--);
return sortStringphp1($stringleft).$sortstring[$middlestring].sortStringphp1($stringright);
}
$givenstring =
sortStringphp1("goodtohaveacoffee");
echo " the sort string : <b>$givenstring</b>";
?>
</body>
</html>
Output:
Conclusion
- The sort string helps categorize the string data as per user requirement and display it on the web application.
- The sort string sorting and display string characters as per the order of the web application and websites.
Recommended Articles
This is a guide to Sort string PHP. Here we discuss How to sort strings in PHP using various ways along with the Examples and outputs. You may also have a look at the following articles to learn more –