Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different ways to sort an array in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 213
    Comment on it

    PHP has a huge collection of predefined functions. This article demonstrates details about some functions which are used to sort an indexed and associative arrays on the basis of keys, values and by using the natural order algorithm. The list and description of each function are given below :

     

    1. Sort by value
    1.1 sort

    sort() function sorts an array into ascending order (alphabetically for letters, numerically for numbers, letters before the numbers).

     

    syntax : sort(array,flags)

    array : Specify the array to sort.(Required)

    flags : Used to define sorting behaviour.(Optional)

     

    flags can have following values :

    SORT_REGULAR : Default flag. Used for comparing items normally.
    SORT_NUMERIC : It used for comparing items numerically.
    SORT_STRING : Compare array values as strings.
    SORT_LOCALE_STRING : Compare elements as strings based on the current locale.
    SORT_NATURAL : Compare items as strings using a natural order algorithm.
    SORT_FLAG_CASE : Sort strings in case-insensitive manner. It can be combined
        SORT_STRING and SORT_NATURAL using (bitwise OR).

     

    Result : sort() return TRUE on success and FALSE on failure.

     

    Note : This function re-index the sorted array. It assigns new keys and removes the older keys instead of reorder them.

     

    Changelog :

    • SORT_LOCALE_STRING flag was added from PHP version 5.0.2.
    • Support for SORT_NATURAL and SORT_FLAG_CASE flag added from version 5.4.0.

     

    Example :

    <?php
    $arr1=array("10","4","57","25","08");
    echo "<pre>";
    sort($arr1);
    print_r($arr1);
    echo "<br>SORT_STRING : <br>"
    sort($arr1,SORT_STRING);
    print_r($arr1);
    echo "<br>SORT_NATURAL : <br>"
    sort($arr1,SORT_NATURAL);
    print_r($arr1);
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [0] => 4
        [1] => 08
        [2] => 10
        [3] => 25
        [4] => 57
    )
    SORT_STRING :
    Array
    (
        [0] => 08
        [1] => 10
        [2] => 25
        [3] => 4
        [4] => 57
    )
    SORT_NATURAL :
    Array
    (
        [0] => 4
        [1] => 08
        [2] => 10
        [3] => 25
        [4] => 57
    )

     

    Warning : sort() function can produce unpredictable results with mixed type values.

     

     

    1.2 rsort

    rsort() is same like sort() function, the only difference is rsort() sorts an array into descending order(highest to lowest).

     

    syntax : rsort(array,sort_flags)

    array : Specify the array to sort.(Required)

    sort_flags : Used to define sorting behaviour.(Optional)

    sort_flags have the same values like sort() function.

     

    Result : rsort() also returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $arr2=array("a" => "orange", "b" => "apple", "c" => "mango", "d" => "pear", "e" => "grapes");
    echo "<pre>";
    rsort($arr2);
    print_r($arr2);
    echo "</pre>";
    ?>

     

    Output :

    Array
    (
        [0] => pear
        [1] => orange
        [2] => mango
        [3] => grapes
        [4] => apple
    )

     

     

    1.3 asort()

    asort() function is mainly used to sort an associative array in ascending order (lowest to highest) with keeping index association. The function sorts an array on the basis of values and it also keep the original key(index) of each element after sorting.

     

    syntax : asort(array, sort_flags)

    array : Specify the array to sort.(Required)

    sort_flags : Used to define sorting behaviour.(Optional)

    sort_flags have the same values like sort() function.

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $arr = array("a" => "orange", "b" => "apple", "c" => "mango", "d" => "pear", "e" => "grapes");
    asort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>

     

    Output :

    Array
    (
        [b] => apple
        [e] => grapes
        [c] => mango
        [a] => orange
        [d] => pear
    )

     

     

    1.4 arsort()

    arsort() function is used to sort an associative array in descending order(highest to lowest) on basis of array values with keeping index association.

     

    syntax : arsort(array, sort_flags)

    array : Specify the array to sort.(Required)

    sort_flags : Used to define sorting behaviour.(Optional)

    sort_flags have the same values like sort() function.

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $arr = array("a" => "orange", "b" => "apple", "c" => "mango", "d" => "pear", "e" => "grapes");
    arsort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [d] => pear
        [a] => orange
        [c] => mango
        [e] => grapes
        [b] => apple
    )

     

     

    2. Sort by key
    2.1 ksort

    ksort() function is used to sort an associative array in ascending order on the basis of the keys. This function takes an associative array and sort according to keys.

     

    syntax : ksort(array, sort_flags)

    array : Specify the array to sort.(Required)

    sort_flags : Used to define sorting behaviour.(Optional)

    sort_flags have the same values like sort() function.

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $arr = array("3" => "orange", "1" => "apple", "5" => "mango", "2" => "pear", "7" => "grapes");
    ksort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [1] => apple
        [2] => pear
        [3] => orange
        [5] => mango
        [7] => grapes
    )

     

     

    2.2 krsort()

    krsort() function is used to sort an associative array in descending order on the basis of the keys.

     

    syntax : krsort(array, sort_flags)

    array : Specify the array to sort.(Required)

    sort_flags : Used to define sorting behaviour.(Optional)

    sort_flags have the same values like sort() function.

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $arr = array("3" => "orange", "1" => "apple", "5" => "mango", "2" => "pear", "7" => "grapes");
    krsort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [7] => grapes
        [5] => mango
        [3] => orange
        [2] => pear
        [1] => apple
    )

     

     

    3. Sort using natural order algorithm
    3.1 natsort

    natsort() sorts an array using a natural order algorithm on the basis of values with maintaining index association. This function uses a sort algorithm which compares alphanumeric strings same as humans with keeping original keys and values.

    The main difference between natsort and regular sorting algorithm(sort) is, in natural algorithm 2 is smaller than 10 while in computer sorting 10 is treated as "10" which is treated as smaller than 2.

     

    syntax : natsort(array)

    array : Specify the array to sort.(Required)

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Changelog : From PHP version 5.2.10, for zero padded numeric strings (e.g., '00005') the function now ignores the 0 padding.

     

    Example :

    <?php
    $arr = array("image12", "image10", "image2", "image1", "image15");
    echo "Using regular sort : <br>";
    sort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    echo "<br>Using natsort : <br>";
    natsort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>

    Output :

    Using regular sort :
    Array
    (
        [0] => image1
        [1] => image10
        [2] => image12
        [3] => image15
        [4] => image2
    )
    Using natsort :
    Array
    (
        [0] => image1
        [4] => image2
        [1] => image10
        [2] => image12
        [3] => image15
    )

     

     

    3.2 natcasesort

    natcasesort() function is a case-insensitive implementation of natsort() function. This function also uses a natural order algorithm to sort an array with keeping index association.

     

    syntax : natcasesort(array)

    array : Specify the array to sort.(Required)

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $arr = array("test12","Test10","test1","Test21","test2");
    echo "<pre>";
    echo "Using sort function : <br>";
    sort($arr);
    print_r($arr);
    echo "Using natsort function : <br>";
    natsort($arr);
    print_r($arr);
    echo "Using natcasesort function : <br>";
    natcasesort($arr);
    print_r($arr);
    echo "</pre>";
    ?>

    Output :

    Using sort function :
    Array
    (
        [0] => Test10
        [1] => Test21
        [2] => test1
        [3] => test12
        [4] => test2
    )
    Using natsort function :
    Array
    (
        [0] => Test10
        [1] => Test21
        [2] => test1
        [4] => test2
        [3] => test12
    )
    Using natcasesort function :
    Array
    (
        [2] => test1
        [4] => test2
        [0] => Test10
        [3] => test12
        [1] => Test21
    )

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: