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:
When you run the program, you will notice that the output is:Code:<?php $nums = array(5,3,2,4,6); asort($nums); foreach ($nums as $num) { echo $num . " "; } ?>
ksort2 3 4 5 6
This method sorts by keys.
Example:
The output is:Code:<?php $nums = array(5,3,2,4,6); asort($nums); foreach ($nums as $num) { echo $num . " "; } ?>
It didn't do anything? Wrong. It sorted it by the keys. Let us think of it as an assocative array.5 3 2 4 6
Notice that the keys are already in order? So the method doesn't change anything.Code:$nums = array( 0 => 5, 1 => 3, 2 => 2, 3 => 4, 4 => 6);
arsort
This method is the same as asort but it sorts the list in descending order.
Output:Code:<?php $nums = array(5,3,2,4,6); arsort($nums); foreach ($nums as $num) { echo $num . " "; } ?>
Notice, that the array is sorted in reverse order? That is what this method did.6 5 4 3 2
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.
The output will be:Code:<?php $nums = array(5,3,2,4,6); krsort($nums); foreach ($nums as $num) { echo $num . " "; }
Again let us look at the associative array.6 4 2 3 5
When we sort the array by the keys, we have reversed the array.Code:$nums = array( 0 => 5, 1 => 3, 2 => 2, 3 => 4, 4 => 6 );
You can apply the same functions to associative arrays where the user has specified different keys.
I'm guessing the sort() rearranges how the keys map to the elements, essentially sorting both and remapping, correct? +rep
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!![]()
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.![]()
The natsort function sorts arrays naturally. You'll need this if you have an array of values that contain text+integers.
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.
Why can't the developers of PHP copy that logic, instead of goto????![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks