php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61887 Behaviour of function given to usort
Submitted: 2012-05-01 07:42 UTC Modified: 2012-05-02 10:12 UTC
From: vorcak dot jan at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2012-05-01 07:42 UTC] vorcak dot jan at gmail dot com
Description:
------------
If you try to sort and array with the custom function using usort, you have to return -1, 0 or 1. It would be nice if user can return any integer and usort function would decide based on the sgn() value

// after few years programming in Python/Java I had problems why my code doesn't work, because I just returned value $a-$b not sgn($a-$b)

Expected result:
----------------
I'd expect usort function to work with any integer, not just -1, 0, 1

Actual result:
--------------
Usort function shuffle the array, it doesn't give me any warning

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-02 10:12 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-05-02 10:12 UTC] johannes@php.net
The documentation says:

  The comparison function must return an integer less than, equal to, or greater
  than zero if the first argument is considered to be respectively less than,
  equal to, or greater than the second.

The implementation is using 

  return retval < 0 ? -1 : retval > 0 ? 1 : 0;

So your request is already fullfilled.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 02:01:28 2024 UTC