User Tools

Site Tools


uphp:library_functions:quick_sort

This is an old revision of the document!


quick_sort

WattmonOS 3.14+

Sort an array using the quick sort algorithm

Library

/lib/uphp/array.inc

Description

$array quick_sort ( array $array_to_sort , int $method [, string $callback ] )

Sort an array by key, value or using a callback function

Parameters

$array_to_sort: Array to be sorted

$method: One of the following methods:

^ Const ^ Value ^ Description ^

SORT_KEY 0 Sort an array by the key
SORT_VALUE 1 Sort an array by the value
SORT_FUNC 2 Sort an array using a callback function

Return Values

array containing the sorted values

Example

Sort an array using a callback to look at the array elements. The array contains a sub-array for each element with the keys 'order' and 'name'.

<pre><? 
 
$unsorted = array();
for ($i=0;$i<10;$i++) {
    $arr=array();
    $arr['order']=rand(0,100);
    $arr['name']='item'.$i;
    $unsorted[]=$arr;
}
 
function compare($a1,$a2) {
    if ($a1['order'] < $a2['order']) return 1;
    return 0;
}
 
$sorted = quick_sort($unsorted, SORT_FUNC,'compare');
 
print_r($sorted); // spew out the sorted array
 
?></pre>
uphp/library_functions/quick_sort.1640594284.txt.gz · Last modified: 2021/12/27 08:38 by admin