Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmPHP | Sort array of strings in natural and standard orders

PHP | Sort array of strings in natural and standard orders

You are given an array of strings. You have to sort the given array in standard way (case of alphabets matters) as well as natural way (alphabet case does not matter).

Input : arr[] = {"Geeks", "for", "neveropen"}
Output : Standard sorting: Geeks for neveropen 
         Natural sorting: for Geeks neveropen 

Input : arr[] = {"Code", "at", "neveropen", "Practice"}
Output : Standard sorting: Code Practice at neveropen 
         Natural sorting: at Code neveropen Practice 

If you are trying to sort the array of string in a simple manner you can simple create a comparison function for character comparison and sort the given array of strings. But that will differentiate lower case and upper case alphabets. To solve this problem if you are opting to solve this in c/java you have to write your own comparison function which specially take care of cases of alphabets. But if we will opt PHP as our language then we can sort it directly with the help of natcasesort(). natcasesort() : It sort strings regardless of their case. Means ‘a’ & ‘A’ are treated smaller than ‘b’ & ‘B’ in this sorting method.

// declare array
$arr = array ("Hello", "to", "neveropen", "for", "GEEks");

// Standard sort
$standard_result = sort($arr);
print_r($standart_result);

// natural sort
$natural_result = natcasesort($arr);
print_r($natural_result);

PHP




<?php
// PHP program to sort an array
// in standard and natural ways.
 
// function to print array
function printArray ($arr)
{
    foreach ($arr as $value) {
        echo "$value ";
    }
}
 
 
// declare array
$arr = array ("Hello", "to", "neveropen", "for", "GEEks");
 
// Standard sort
$standard_result = $arr;
sort($standard_result);
echo "Array after Standard sorting: ";
printArray($standard_result);
 
// natural sort
$natural_result = $arr;
natcasesort($natural_result);
echo "\nArray after Natural sorting: ";
printArray($natural_result);
?>


Output:

Array after Standard sorting: GEEks Hello for neveropen to 
Array after Natural sorting: for neveropen GEEks Hello to 
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments