ArrayObject::natsort


Sort entries using a natural order algorithm.



<?php

void 
public ArrayObject::natsort ( void )

?>

 ATTENTION 


This method implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations, described as a natural ordering.

This method is significantly different from the regular ordering used by ArrayObject::asort.



  1 EXERCISE   

<?php

$Dwarfs 
= [ 1 => 'Bashful', 2 => 'Doc', 
                  
3 => 'Grumpy', 4 => 'Happy', 
                  
5 => 'Sneezy', 6 => 'Sleepy', 7 => 'Dopey'];

$dwArrayObject = new ArrayObject($Dwarfs);

$dwArrayObject->natsort();

foreach(
$dwArrayObject as $dw => $dwc)
{
echo 
'[ ' . $dw . ' ] - ';
var_dump($dw, $dwc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 1 ] - int(1) string(7) "Bashful"

[ 2 ] - int(2) string(3) "Doc"

[ 7 ] - int(3) string(6) "Grumpy"

[ 3 ] - int(4) string(5) "Happy"

[ 4 ] - int(5) string(6) "Sneezy"

[ 6 ] - int(6) string(6) "Sleepy"

[ 5 ] - int(7) string(5) "Dopey"


  2 EXERCISE   

<?php

$Ellayers 
= [ 'K' => 2, 'L' => 8, 'M' => 18, 'N' => 32, 'O' => 32, 'P' => 18, 'Q' => 8];

$elArrayObject = new ArrayObject($Ellayers);

$elArrayObject->natsort();

foreach(
$elArrayObject as $el => $elc)
{
echo 
'[ ' . $el . ' ] - ';
var_dump($el, $elc);
echo 
'<br><br>';
}

?>

 RESULT   

[ K ] - string(1) "K" int(2)

[ L ] - string(1) "L" int(8)

[ M ] - string(1) "M" int(18)

[ N ] - string(1) "N" int(32)"

[ O ] - string(1) "O" int(32)

[ P ] - string(1) "P" int(18)

[ Q ] - string(1) "Q" int(8)


  3 EXERCISE   

<?php

$Conts 
= [ 'c' => 299792458, 'G' => 6.67428E-11 ];  

$ctArrayObject = new ArrayObject($Conts);

$ctArrayObject->natsort();

foreach(
$ctArrayObject as $ct => $ctc)
{
echo 
'[ ' . $ct . ' ] - ';
var_dump($ct, $ctc);
echo 
'<br><br>';
}

?>

 RESULT   

[ G ] - string(1) "G" float(6.67428E-11)

[ c ] - string(1) "c" int(299792458)


  4 EXERCISE   

<?php

$Cout_cont 
= [  'JPN' => "Japan", 'BRA' => "Brasil", 'POR' => "Portugal" ];  

$ccArrayObject = new ArrayObject($Cout_cont);

$ccArrayObject->natsort();

foreach(
$ccArrayObject as $cc => $ccc)
{
echo 
'[ ' . $cc . ' ] - ';
var_dump($cc, $ccc);
echo 
'<br><br>';
}

?>

 RESULT   

[ BRA ] - string(3) "BRA" string(6) "Brasil"

[ JPN ] - string(3) "JPN" string(5) "Japan"

[ POR ] - string(3) "POR" string(8) "Portugal"


  5 EXERCISE   

<?php

$nmValues 
= [ 234, 130, 22, -12, 0.23456, 9.999 ];  

$nmArrayObject = new ArrayObject($nmValues);

$nmArrayObject->natsort();

foreach(
$nmArrayObject as $nm => $nmc)
{
echo 
'[ ' . $nm . ' ] - ';
var_dump($nm, $nmc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 3 ] - int(3) int(-12)

[ 4 ] - int(4) float(0.23456)

[ 5 ] - int(5) float(9.999)

[ 2 ] - int(2) int(22)

[ 1 ] - int(1) int(130)

[ 0 ] - int(0) int(234)


  6 EXERCISE   

<?php

$mvValues 
= [ 'two hundred thirty-four', 234, 
                      
'One hundred and thirty', 130, 
                      
'minus one hundred and twelve', -12, 
                      
'Nine point nine hundred and ninety nine', 9.999 ];  

$mvArrayObject = new ArrayObject($mvValues);

$mvArrayObject->natsort();

foreach(
$mvArrayObject as $mv => $mvc)
{
echo 
'[ ' . $mv . ' ] - ';
var_dump($mv, $mvc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 5 ] - int(5) int(-12)

[ 7 ] - int(7) float(9.999)

[ 3 ] - int(3) int(130)

[ 1 ] - int(1) int(234)

[ 6 ] - int(6) string(39) "Nine point nine hundred and ninety nine"

[ 2 ] - int(2) string(22) "One hundred and thirty"

[ 4 ] - int(4) string(28) "minus one hundred and twelve"

[ 0 ] - int(0) string(23) "two hundred thirty-four"


  7 EXERCISE   

<?php

$nvValues 
= [ 1 => 'two hundred thirty-four', 3 => 234, 
                      
2 => 'One hundred and thirty', 5 => 130, 
                      
4 => 'minus one hundred and twelve', 7 => -12, 
                      
8 => 'Nine point nine hundred and ninety nine', 6 => 9.999 ];  

$nvArrayObject = new ArrayObject($nvValues);

$nvArrayObject->natsort();

foreach(
$nvArrayObject as $nv => $nvc)
{
echo 
'[ ' . $nv . ' ] - ';
var_dump($nv, $nvc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 7 ] - int(7) int(-12)

[ 6 ] - int(6) float(9.999)

[ 5 ] - int(5) int(130)

[ 3 ] - int(3) int(234)

[ 8 ] - int(8) string(39) "Nine point nine hundred and ninety nine"

[ 2 ] - int(2) string(22) "One hundred and thirty")

[ 4 ] - int(4) string(28) "minus one hundred and twelve"

[ 1 ] - int(1) string(23) "two hundred thirty-four"


  8 EXERCISE   

<?php

$ovValues 
= [ 1, 2, 3, 3 => 'x', 6 => 'X', 4 => 'b', 5 => 'c', 7 => 'A' ];  

$ovArrayObject = new ArrayObject($ovValues);

$ovArrayObject->natsort();

foreach(
$ovArrayObject as $ov => $ovc)
{
echo 
'[ ' . $ov . ' ] - ';
var_dump($ov, $ovc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 0 ] - int(0) int(1)

[ 1 ] - int(1) int(2)

[ 2 ] - int(2) int(3)

[ 7 ] - int(7) string(1) "A"

[ 6 ] - int(6) string(1) "X"

[ 4 ] - int(4) string(1) "b"

[ 5 ] - int(5) string(1) "c"

[ 3 ] - int(3) string(1) "x"


  9 EXERCISE   

<?php

$otValues 
= [ 'one', 'two', 'three'];

$pvValues = [ 1, 2, 3, ...$otValues ];  

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        The Unpacking inside arrays: 
              ...$otValues, 
        was introduced in PHP 7.4.0 

   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$pvArrayObject = new ArrayObject($pvValues);

$pvArrayObject->natsort();

foreach(
$pvArrayObject as $pv => $pvc)
{
echo 
'[ ' . $pv . ' ] - ';
var_dump($pv, $pvc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 0 ] - int(0) int(1)

[ 1 ] - int(1) int(2)

[ 2 ] - int(2) int(3)

[ 3 ] - int(3) string(3) "one"

[ 5 ] - int(5) string(5) "three"

[ 4 ] - int(4) string(3) "two"