My previous article "Different ways to sort an array in PHP" provides the list and details about different sorting functions which are used for sorting arrays. These functions use the internal function for comparison. This article demonstrates some other sorting functions which sorts array using user defined function. These functions uses a user function for comparison.
The comparison function takes two parameters ($a,$b) and it must return an integer less than, equal to or greater than 0 by comparing first parameter with second parameter, like this -
case 1 : if $a == $b, return 0.
case 2 : if $a > $b, return 1.
case 3 : if $a < $b, return -1.
Based on above cases i have created a comparison function for sorting arrays in both ascending and descending order.
a. For ascending order :
//----function to sort in ascending order------
function compare_asc($a,$b)
{
if ($a==$b){
return 0;
} else {
return ($a>$b)?1:-1;
}
}
b. For descending order
//----function to sort in descending order------
function compare_desc($a,$b)
{
if ($a==$b){
return 0;
} else {
return ($a<$b)?1:-1;
}
}
The list and details of PHP functions which used these comparison functions to sort an array are given below :
1. usort()
usort() function is used to sort an array on the basis of values by using a user defined function. This function assigns new keys for the elements it does not keep the original keys.
Syntax : usort(array, user_function)
array : Specify the array to sort.(Required)
user_function : Specifies a user defined function to compare array values.
Result : usort() returns TRUE on success and FALSE on failure.
Example :
<?php
$dates = array("2016-10-12 10:12:25","2016-07-02 15:03:45","2016-01-28 05:22:05","2017-04-15 03:30:00","2016-11-04 06:15:55");
echo "<pre>";
echo "sort dates in ascending order : <br>";
usort($dates,"compare_asc");
print_r($dates);
echo "sort date in descending order : <br>";
usort($dates,"compare_desc");
print_r($dates);
echo "</pre>";
?>
Output :
sort dates in ascending order :
Array
(
[0] => 2016-01-28 05:22:05
[1] => 2016-07-02 15:03:45
[2] => 2016-10-12 10:12:25
[3] => 2016-11-04 06:15:55
[4] => 2017-04-15 03:30:00
)
sort date in descending order :
Array
(
[0] => 2017-04-15 03:30:00
[1] => 2016-11-04 06:15:55
[2] => 2016-10-12 10:12:25
[3] => 2016-07-02 15:03:45
[4] => 2016-01-28 05:22:05
)
2. uasort()
uasort() function sorts an array on the basis of values using a user defined function. This function also maintains the original keys after sorting. This function is mainly used for sorting associative arrays where original keys keep unchanged.
Syntax : uasort(array,user_function)
array : Specify the array to sort.(Required)
user_function : Specifies a user defined function to compare array values.
Result : This function returns TRUE on success and FALSE on failure.
Example :
<?php
$array = array("a"=>5,"b"=>2,"c"=>7,"d"=>1,"e"=>4);
echo "<pre>";
echo "sort in ascending order : <br>";
uasort($array,"compare_asc");
print_r(array);
echo "sort in descending order : <br>";
uasort($array,"compare_desc");
print_r(array);
echo "</pre>";
?>
Output :
sort in ascending order :
Array
(
[d] => 1
[b] => 2
[e] => 4
[a] => 5
[c] => 7
)
sort in descending order :
Array
(
[c] => 7
[a] => 5
[e] => 4
[b] => 2
[d] => 1
)
3. uksort()
uksort() sorts an array on the basis of keys by using a user defined function for comparing keys. It also maintains the index association.
Syntax : uksort(array,user_function)
array : Specify the array to sort.(Required)
user_function : Specifies a user defined function to compare array keys.
Result : This function returns TRUE on success and FALSE on failure.
Example :
<?php
$array=array("d" => "orange", "b" => "apple", "e" => "mango", "a" => "pear", "c" => "grapes");
echo "<pre>";
echo "sort in ascending order : <br>";
uksort($array,"compare_asc");
print_r($array);
echo "sort in descending order : <br>";
uksort($array,"compare_desc");
print_r($array);
echo "</pre>";
?>
Output :
sort in ascending order :
Array
(
[a] => pear
[b] => apple
[c] => grapes
[d] => orange
[e] => mango
)
sort in descending order :
Array
(
[e] => mango
[d] => orange
[c] => grapes
[b] => apple
[a] => pear
)
0 Comment(s)