Home > Backend Development > PHP Tutorial > Sort string PHP

Sort string PHP

王林
Release: 2024-08-29 13:05:19
Original
612 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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';
Copy after login
  • Convert the string into an array using the string split method.
$stringndarray = str_split($sortstring);
Copy after login
  • 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);
Copy after login
  • Use the rsort() method to sort the string as per descending order.
rsort($stringndarray);
Copy after login
  • Convert an array into the string using the implode method.
$stringndarray = implode($stringndarray);
Copy after login
  • Return the sorting string in the PHP language.
echo $stringndarray;
Copy after login

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>
Copy after login

Output:

Sort string PHP

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…}
Copy after login
  • Create a string variable to sort the string.
$sortstring = 'jhjabcdewyxdef';
Copy after login
  • Create a second-string variable to know the length of the given string.
$stringlength;
Copy after login
  • Create a third-string variable to know the position of the string element and initialize with zero.
$currentposition;
Copy after login
  • Place these string variables in the function as arguments.
function sortStringphp(&$sortstring, $stringlength, $currentposition=0) {
write code here…
}
Copy after login
  • If the string current position and length of the string are equal then return the string.
if($currentposition == $stringlength){ return; }
Copy after login
  • Create the one variable to increments the character of the string and initialize it.
$nextposition = $currentposition + 1;
Copy after login
  • 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++;
}
Copy after login
  • Use the recursive function to avoid recursion inside of the main function.
sortStringphp($sortstring, $stringlength, $currentposition+1);
Copy after login
  • Return the sort string in the PHP.
sortStringphp($sortstring,strlen($sortstring));
echo $sortstring;
Copy after login

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>
Copy after login

Output:

Sort string PHP

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 = '';
Copy after login
  • The variable creates to measure the length of the string.
$stringlength = strlen($sortstring)-1 ;
Copy after login
  • If the length of the string is equal to zero then return the string and stop sorting.
if ($stringlength <= 0) {
return $sortstring;
}
Copy after login
  • Make a variable to create the middle of the string (pivot).
$pivot = floor($stringlength/2);
Copy after login
  • Use the do-while loop for the sorting algorithm.
do{
write sort string code here..
}while(sort string condition…)
Copy after login
  • If length of the string and middle string variable is equal then stop the break and continue the next loop.
if ($stringlength == $middlestring){
continue;
}
Copy after login
  • Use ascending or descending sorting logic as per requirement.
if ($sortstring[$stringlength] >= $sortstring[$middlestring]) {
$stringleft = $stringleft.$sortstring[$stringlength];
} else {
$stringright = $stringright.$sortstring[$stringlength];
}
Copy after login
  • Return the sorting string according to the algorithm.
return sortStringphp($stringleft).$sortstring[$middlestring].sortStringphp($stringright);
Copy after login
  • print the sort string in the php.
$givenstring = sortStringphp ("goodtohaveacoffee");
echo " the sort string : <b>$givenstring</b>"
Copy after login

Example:




Ascending Order

$givenstring"; ?>

Descending Order

$givenstring"; ?>
Copy after login

Output:

Sort string PHP

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.

The above is the detailed content of Sort string PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template