+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP and Sorting

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    PHP and Sorting

    Sorting in PHP

    PHP provides a large variety of functions for sorting arrays.

    The sort functions are:
    • asort
    • ksort
    • arsort
    • krsort
    • sort

    The asort method sorts an array by its values. The ksort method sorts an array by it's keys. The arsort and krsort sort arrays in descending order where

    as the asort and ksort functions sort an array in ascending order.

    The sort function doesn't make any attempt to preserve the value of the keys. So we will use the asort, ksort, arsort, and krsort methods.

    asort

    This method sorts the array by values.

    Example:

    Code:
    <?php
    $nums = array(5,3,2,4,6);
    asort($nums);
    
    foreach ($nums as $num) {
    	echo $num . " ";
    }
    ?>
    When you run the program, you will notice that the output is:

    2 3 4 5 6
    ksort

    This method sorts by keys.

    Example:

    Code:
    <?php
    $nums = array(5,3,2,4,6);
    asort($nums);
    
    foreach ($nums as $num) {
    	echo $num . " ";
    }
    ?>
    The output is:

    5 3 2 4 6
    It didn't do anything? Wrong. It sorted it by the keys. Let us think of it as an assocative array.

    Code:
    $nums = array(
    0 => 5,
    1 => 3,
    2 => 2,
    3 => 4,
    4 => 6);
    Notice that the keys are already in order? So the method doesn't change anything.

    arsort

    This method is the same as asort but it sorts the list in descending order.

    Code:
    <?php
    $nums = array(5,3,2,4,6);
    arsort($nums);
    
    foreach ($nums as $num) {
    	echo $num . " ";
    }
    ?>
    Output:

    6 5 4 3 2
    Notice, that the array is sorted in reverse order? That is what this method did.

    krsort

    When we look at this method, we will notice that the array isn't actually sorted. Well we sorted the keys of the arrays.

    Code:
    <?php
    $nums = array(5,3,2,4,6);
    krsort($nums);
    
    foreach ($nums as $num) {
    	echo $num . " ";
    }
    The output will be:

    6 4 2 3 5
    Again let us look at the associative array.

    Code:
    $nums = array(
    0 => 5,
    1 => 3,
    2 => 2,
    3 => 4,
    4 => 6
    );
    When we sort the array by the keys, we have reversed the array.


    You can apply the same functions to associative arrays where the user has specified different keys.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: PHP and Sorting

    I'm guessing the sort() rearranges how the keys map to the elements, essentially sorting both and remapping, correct? +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Jordan Guest

    Re: PHP and Sorting

    No, sort only sorts the values from highest to lowest. If the values are not numerical, sort sorts them alphabetically. sort() completely obliterates the index values so this array:

    a => Z
    b => A

    will become

    0 => A
    1 => Z

    after passed to sort. There is natsort() which is a natural sorting algorithm. sort() will place "pre10" after "pre1" and before "pre2", "pre20" and "pre2" and before "pre3", etc. To correct this, use natsort.

    An important note, asort and arsort both sort the array by value and maintain index association. IE: in the above example b => a and a=> z after sorting.

    In the end, all of these sorting methods are ridiculous. There should be one sort method that takes arguments to sort differently rather than 10 different sort methods. These were a pain in the ass to learn for the zend certification and they do appear on the test.

    Anyway, +rep!

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: PHP and Sorting

    What is the natural sorting algorithm? What does that do?

    I agree it is a huge pain to have 10 sorting methods. In Java and C++, there is 1 that I know of.

  6. #5
    Jordan Guest

    Re: PHP and Sorting

    The natsort function sorts arrays naturally. You'll need this if you have an array of values that contain text+integers.

  7. #6
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: PHP and Sorting

    C++ allows you to specify a comparator function that can be passed to sort as a second parameter. The result is you can sort based on any ordering you can think of.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Jordan Guest

    Re: PHP and Sorting

    Why can't the developers of PHP copy that logic, instead of goto????

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting issue
    By Wintermute in forum C and C++
    Replies: 5
    Last Post: 04-18-2011, 07:49 AM
  2. Sorting
    By unstoppable in forum General Programming
    Replies: 3
    Last Post: 03-15-2011, 08:38 AM
  3. Sorting efficiency
    By chili5 in forum Programming Theory
    Replies: 9
    Last Post: 07-19-2010, 02:14 PM
  4. sorting lists
    By ghostrider_ in forum C and C++
    Replies: 2
    Last Post: 04-27-2010, 06:32 AM
  5. Sorting In PHP vs. DB
    By BASHERS33 in forum PHP Development
    Replies: 4
    Last Post: 07-31-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts