PHP » PYTHON |
login |
register |
about
|
In Python 2.4+, you should sort items by a "key" associated with each element. You provide a function which computes the key for each element.
array.sort(key=keyfunc) result = sorted(array, key=keyfunc) # does not change input array In Python 2.x, you could also use a comparator function, but this feature has been removed in Python 3, and so should no longer be used: array.sort(cmp=cmpfunc) result = sorted(array, cmp=cmpfunc) # does not change input array usort(PHP 4, PHP 5) usort — Sort an array by values using a user-defined comparison function DescriptionThis function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.
Parameters
Return ValuesReturns TRUE on success or FALSE on failure. Changelog
Examples
Example #1 usort() example
<?php The above example will output: 0: 1 1: 2 2: 3 3: 5 4: 6
Example #2 usort() example using multi-dimensional array
<?php When sorting a multi-dimensional array, $a and $b contain references to the first index of the array. The above example will output: $fruits[0]: apples $fruits[1]: grapes $fruits[2]: lemons
Example #3 usort() example using a member function of an object
<?php The above example will output: b c d
See Also
|
more
Recently updated
more
Most requested
more
Last requests
|