SQL PHP HTML ASP JavaScript articles and free scripts to download
 

usort: sorting by user defined function


We can sort an array in PHP by using a user defined comparison function. In the function usort we can use our own sort criteria by using one function. This way we get the flexibility to sort the array by using some non-trivial criteria. You can read many standard way of sorting the array given here.

Here is the user defined function has to have some matching requirements. One is it has to accept two variables. It should return either 0,less than 0 or greater than 0 based on the matching where first element is less than , equal to or greater than second element.

Using this now let us try to sort one array. Here the input array has some words and we will arrange the elements in the order of their length. So after the sorting the small word ( by length ) will be at the first and the longest word will to the end.

This usort function does not retain the associated index or the key of the elements of the array.

Here is the code for the above script. The output of the code is given below that.
<?
function compare($x,$y){
$x=strlen($x);
$y=strlen($y);
if($x==$y){
return 0;
}
return ($x < $y) ? -1 : 1;

}

$input = array ("Pinaple", "Orange", "Banana", "Mango", "Apple","Strawberry","aaa","bb");
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
usort($input, "compare");
echo "<br>-----After usort---------<br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
Here is the output of usort function where user defined comparisons function is used

0 -> Pinaple
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry
6 -> aaa
7 -> bb

-----After usort---------
0 -> bb
1 -> aaa
2 -> Apple
3 -> Mango
4 -> Orange
5 -> Banana
6 -> Pinaple
7 -> Strawberry


Further readings
 PHP array
Array functions in PHP
array : Creating an Array
array_diff Difference of two arrays
array_count_values counting the frequency of values inside an array
count : sizeof Array Size or length
array_push : Adding element to an Array
array_sum : Array Sum of all elements
Displaying the elements of an array
Array checkbox
in_array : IN Array to search for elements inside an array
array_rand : Random elements of Array
array_unique : Array Unique values
Breaking of strings to create array using split command
Session Array to maintain data in different pages
unset : Deleting elements of an array by using its key or value
sort: Simple sorting of PHP array
rsort: Reverse sorting of PHP array
asort: Maintaining index of the elements after sorting
arsort: Sorting in reverse order for a PHP array
ksort: Array key Sorting in PHP
krsort: Array reverse key Sorting in PHP
usort: Sorting array by using user defined function


























Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Array Functions
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.