Req #61887 [Opn->Nab]: Behaviour of function given to usort

From: Date: Wed, 02 May 2012 10:12:41 +0000
Subject: Req #61887 [Opn->Nab]: Behaviour of function given to usort
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170083@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61887&edit=1

 ID:                 61887
 Updated by:         johannes@php.net
 Reported by:        vorcak dot jan at gmail dot com
 Summary:            Behaviour of function given to usort
-Status:             Open
+Status:             Not a bug
 Type:               Feature/Change Request
 Package:            *General Issues
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2012-05-01 07:42:22] 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


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61887&edit=1


Thread (2 messages)

« previous php.bugs (#170083) next »