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!





26 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
  5. After reading this blog i am very strong and clear in this topic.. explanation are step by step so easy to understand

    php training institute | best php training institute | best php training institute in chennai | php training institute in chennai

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

    ReplyDelete
  7. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.
    PHP Training in Chennai

    ReplyDelete
  8. krsort is just what i needed

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

    ReplyDelete

  10. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

    Android App Development Company

    ReplyDelete
  11. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    PHP Training in Chennai

    ReplyDelete
  12. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  13. Good Blogpost. Really helpful information thanks for this post. keep continue the same thing. learn PHP training in Bangalore

    ReplyDelete
  14. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  15. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  16. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  17. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  18. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.more 
    php jobs in hyderabad.

    ReplyDelete
  19. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Picture printing online
    Order photo prints online
    Photo printing sites

    ReplyDelete