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.
def uksort(d, keyfunc): return [(k, d[k]) for k in sorted(d.keys(), key=keyfunc)] 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: def uksort(d, cmpfunc): return [(k, d[k]) for k in sorted(d.keys(), cmp=cmpfunc)] uksort(PHP 4, PHP 5) uksort — Sort an array by keys using a user-defined comparison function Descriptionuksort() will sort the keys of an array 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. Examples
Example #1 uksort() example
<?phpThe above example will output: an apple: 3 a banana: 4 the Earth: 2 John: 1
See Also
|
more
Recently updated
more
Most requested
|