Thursday, 29 April 2010

Explain sort(), rsort(), asort(), arsort(), ksort(), krsort() and natsort()?

Suppose we have an array like below:

$fruits = array("lemon""orange""banana""apple");

1. sort()       
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
e.g.,
sort($fruits);

The above example will output:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange 


2. rsort()            
This function sorts an array in reverse order (highest to lowest).
Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.
 e.g.,
rsort($fruits);


The above example will output:
0 = orange
1 = lemon
2 = banana
3 = apple


3. asort()              
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.
e.g.,
asort($fruits);

The above example will output:
c = apple
b = banana
d = lemon
a = orange

4. arsort()               
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.
e.g.,
arsort($fruits);

The above example will output:
a = orange
d = lemon
b = banana
c = apple
 
5. ksort()
               Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.
e.g.,
ksort($fruits);

The above example will output:
a = orange
b = banana
c = apple
d = lemon

6. krsort()               
Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays.
e.g.,
krsort($fruits);

The above example will output:
d = lemon
c = apple
b = banana
a = orange
 
7. natsort()
                 This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering".

e.g., 
<?php
$array1 
$array2 = array("img12.png""img10.png""img2.png""img1.png");
sort($array1);
echo 
"Standard sorting\n";print_r($array1);
natsort($array2);
echo 
"\nNatural order sorting\n";print_r($array2);?>  


The above example will output:
Standard sorting
Array
(
    [0] => img1.png
    [1] => img10.png
    [2] => img12.png
    [3] => img2.png
)

Natural order sorting
Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)



Cheers!





11 comments:

  1. hey alex! no there is no date sort function in php. php does not have built - in function for the same. but you can do with uksort function. example given below:

    'some text', '2008-03-06' => 'some text');

    function cmp($a, $b)
    {
    global $array;
    return strcmp($array[$a]['db'], $array[$b]['db']);
    }

    uksort($array, 'cmp');

    print_r($array);
    ?>

    ReplyDelete
  2. nice sorting fun

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Nice it seems to be good post... It will get readers engagement on the article since readres engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    iOS Training in Chennai
    Android Training in Chennai
    php Training in Chennai

    ReplyDelete