The Most Active and Friendliest
Affiliate Marketing Community Online!

“Adavice”/  “1Win

Tip : Sorting arrays in PHP

D

dman_2007

Guest
You can use following set of functions to sort arrays in php :

1) sort & rsort

Sort takes array to be sorted as its first required parameter and sort_flags as its second optuional parameter. It sorts the array as well as reindexes it assigning elements 0 based numeric keys and removing any keys assigned prior to sorting. sort_flags parameter can be used to specify how the array is to be sorted, possible values are :

a) SORT_REGULAR : regular sorting, items are compared normally and types are not changed
b) SORT_NUMERIC : array elements are compared numerically
c) SORT_STRING : array elements are compared as strings
d) SORT_LOCALE_STRING : array elements are compared as string based on the current locale

rsort function is similar to sort function except that it sorts the array inreverse order i.e. highest to lowest.


2) asort & arsort

asort and arsort is basically used for sorting associative array. The main difference between sort and asort is that array elements retain their original keys and array is not reindexed.

3) ksort & krsort

Similar asort and arsort functions, the only difference is that the array is sorted based on the key values instead of element values.

4) usort & uksort

These functions are similar sort and ksort in functionality. Only difference is that you can specify a custom callback function as second parameter to specify the custom ordering of the array elements.

5) natsort & natcasesort

natsort function sorts alphanumeric array elements as humans would do known as natural ordering (for example, 'img2' ahead of 'img10') while maintaining the key value relationship of array elements. natcasesort is the case insensitive version of the natsort function.
 
multisort :-
syntax:- array_multisort(array1,sorting order,sorting type,array2,array3...)
usort:-
The usort() function sorts an array by a user defined comparison function.
syntax:- usort(array,sorttype);
_______________________________________________________________
Web site Design
 
Last edited by a moderator:
Hi

This is an implementation of Quicksort in PHP. This algorithm is recursive.

Code:
[COLOR=#808080][I]// Recursive version:[/I][/COLOR]
[COLOR=#000000][B]function[/B][/COLOR] quickSortRecursive[COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$arr[/COLOR], [COLOR=#0000ff]$left[/COLOR] = [COLOR=#cc66cc]0[/COLOR] , [COLOR=#0000ff]$right[/COLOR] = [COLOR=#000000][B]NULL[/B][/COLOR] [COLOR=#66cc66])[/COLOR]
[COLOR=#66cc66]{[/COLOR]
[COLOR=#808080][I]// when the call is recursive we need to change[/I][/COLOR]
[COLOR=#808080][I]//the array passed to the function yearlier[/I][/COLOR]
[COLOR=#000066]static[/COLOR] [COLOR=#0000ff]$array[/COLOR] = [COLOR=#000066]array[/COLOR][COLOR=#66cc66]([/COLOR][COLOR=#66cc66])[/COLOR];
[COLOR=#b1b100]if[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$right[/COLOR] == [COLOR=#000000][B]NULL[/B][/COLOR] [COLOR=#66cc66])[/COLOR]
[COLOR=#0000ff]$array[/COLOR] = [COLOR=#0000ff]$arr[/COLOR];
 
[COLOR=#b1b100]if[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$right[/COLOR] == [COLOR=#000000][B]NULL[/B][/COLOR] [COLOR=#66cc66])[/COLOR]
[COLOR=#0000ff]$right[/COLOR] = [COLOR=#000066]count[/COLOR][COLOR=#66cc66]([/COLOR][COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66])[/COLOR][COLOR=#cc66cc]-1[/COLOR];[COLOR=#808080][I]//last element of the array[/I][/COLOR]
 
[COLOR=#0000ff]$i[/COLOR] = [COLOR=#0000ff]$left[/COLOR];
[COLOR=#0000ff]$j[/COLOR] = [COLOR=#0000ff]$right[/COLOR];
 
[COLOR=#0000ff]$tmp[/COLOR] = [COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#66cc66]([/COLOR]int[COLOR=#66cc66])[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#66cc66]([/COLOR][COLOR=#0000ff]$left[/COLOR]+[COLOR=#0000ff]$right[/COLOR][COLOR=#66cc66])[/COLOR]/[COLOR=#cc66cc]2[/COLOR] [COLOR=#66cc66])[/COLOR][COLOR=#66cc66]][/COLOR];
 
[COLOR=#808080][I]// partion the array in two parts.[/I][/COLOR]
[COLOR=#808080][I]// left from $tmp are with smaller values,[/I][/COLOR]
[COLOR=#808080][I]// right from $tmp are with bigger ones[/I][/COLOR]
[COLOR=#b1b100]do[/COLOR]
[COLOR=#66cc66]{[/COLOR]
[COLOR=#b1b100]while[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#0000ff]$i[/COLOR][COLOR=#66cc66]][/COLOR] < [COLOR=#0000ff]$tmp[/COLOR] [COLOR=#66cc66])[/COLOR]
[COLOR=#0000ff]$i[/COLOR]++;
 
[COLOR=#b1b100]while[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$tmp[/COLOR] < [COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#0000ff]$j[/COLOR][COLOR=#66cc66]][/COLOR] [COLOR=#66cc66])[/COLOR]
[COLOR=#0000ff]$j[/COLOR]--;
 
[COLOR=#808080][I]// swap elements from the two sides[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$i[/COLOR] <= [COLOR=#0000ff]$j[/COLOR] [COLOR=#66cc66])[/COLOR]
[COLOR=#66cc66]{[/COLOR]
[COLOR=#0000ff]$w[/COLOR] = [COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#0000ff]$i[/COLOR][COLOR=#66cc66]][/COLOR];
[COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#0000ff]$i[/COLOR][COLOR=#66cc66]][/COLOR] = [COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#0000ff]$j[/COLOR][COLOR=#66cc66]][/COLOR];
[COLOR=#0000ff]$array[/COLOR][COLOR=#66cc66][[/COLOR][COLOR=#0000ff]$j[/COLOR][COLOR=#66cc66]][/COLOR] = [COLOR=#0000ff]$w[/COLOR];
 
[COLOR=#0000ff]$i[/COLOR]++;
[COLOR=#0000ff]$j[/COLOR]--;
[COLOR=#66cc66]}[/COLOR]
[COLOR=#66cc66]}[/COLOR][COLOR=#b1b100]while[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$i[/COLOR] <= [COLOR=#0000ff]$j[/COLOR] [COLOR=#66cc66])[/COLOR];
 
[COLOR=#808080][I]// devide left side if it is longer the 1 element[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$left[/COLOR] < [COLOR=#0000ff]$j[/COLOR] [COLOR=#66cc66])[/COLOR]
quickSortRecursive[COLOR=#66cc66]([/COLOR][COLOR=#000000][B]NULL[/B][/COLOR], [COLOR=#0000ff]$left[/COLOR], [COLOR=#0000ff]$j[/COLOR][COLOR=#66cc66])[/COLOR];
 
[COLOR=#808080][I]// the same with the right side[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR][COLOR=#66cc66]([/COLOR] [COLOR=#0000ff]$i[/COLOR] < [COLOR=#0000ff]$right[/COLOR] [COLOR=#66cc66])[/COLOR]
quickSortRecursive[COLOR=#66cc66]([/COLOR][COLOR=#000000][B]NULL[/B][/COLOR], [COLOR=#0000ff]$i[/COLOR], [COLOR=#0000ff]$right[/COLOR][COLOR=#66cc66])[/COLOR];
 
[COLOR=#808080][I]// when all partitions have one element[/I][/COLOR]
[COLOR=#808080][I]// the array is sorted[/I][/COLOR]
 
[COLOR=#b1b100]return[/COLOR] [COLOR=#0000ff]$array[/COLOR];
[COLOR=#66cc66]}[/COLOR]
 
banners
Back