usort() Behavior?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Little

    usort() Behavior?

    I wrote a sorting function to use with usort(). It worked fine, and I
    went on with the development of my code. At one point I changed the name
    of the sorting function, but forgot to update it in the usort() call.
    Yet, it still was working. I removed the usort() call to make sure my
    data didn't just happen to be arriving in sorted order, and it wasn't.

    When I removed the usort() call, the code broke; when I put it back, it
    worked. I changed the name of the callback function in the usort() call
    to random letters, and it still worked. How is this possible? Does usort
    "remember" the last valid sorting algorithm it had? To test this wild
    hypothesis, I uploaded the code to a server where it had never run. It
    still works. WTH?

    The elements of the array I'm sorting are each an array with two
    elements, and I'm sorting on the second element, in reverse order. Is
    this the default behavior for usort() if it can't find the callback
    function? This is really bizarre. Anyone know what's going on?

    --
    Alan Little
    Phorm PHP Form Processor

  • Pedro Graca

    #2
    Re: usort() Behavior?

    Alan Little wrote:
    [edited][color=blue]
    > I wrote a sorting function to use with usort().[/color]
    [...][color=blue]
    > changed the name
    > of the sorting function, but forgot to update it in the usort() call.
    > Yet, it still was working.[/color]
    [...][color=blue]
    > I changed the name of the callback function in the usort() call
    > to random letters, and it still worked.[/color]

    Post the part of your code that calls usort(), the callback comparison
    function.


    Additionally, insert

    error_reporting (E_ALL);
    ini_set('displa y_errors', '1');

    at the top of your code.
    --
    Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
    == ** ## !! !! ## ** ==
    TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
    bypass the spam filter. I will answer all pertinent mails from a valid address.

    Comment

    • Alan Little

      #3
      Re: usort() Behavior?

      Carved in mystic runes upon the very living rock, the last words of Pedro
      Graca of comp.lang.php make plain:
      [color=blue]
      > Alan Little wrote:
      > [edited][color=green]
      >> I wrote a sorting function to use with usort().[/color]
      > [...][color=green]
      >> changed the name
      >> of the sorting function, but forgot to update it in the usort() call.
      >> Yet, it still was working.[/color]
      > [...][color=green]
      >> I changed the name of the callback function in the usort() call
      >> to random letters, and it still worked.[/color]
      >
      > Post the part of your code that calls usort(), the callback comparison
      > function.
      >
      > Additionally, insert
      >
      > error_reporting (E_ALL);
      > ini_set('displa y_errors', '1');
      >
      > at the top of your code.[/color]

      OK, I completely isolated the code, set up some test data, and tried it
      again. Here's the sorting function:

      function TestSort($a, $b) {
      if ($a[1] == $b[1]) return 0;
      return ($a[1] > $b[1]) ? -1 : 1;
      }

      Here's some sample data:

      $Data = array (
      array ('Some Data 1', 56),
      array ('Some Data 2', 78),
      array ('Some Data 3', 25),
      array ('Some Data 4', 43)
      );

      Calling usort() with 'TestSort' gives the expected results. Calling it
      with 'xxx' gives me a reversed array:

      0
      0 -> Some Data 4
      1 -> 43
      1
      0 -> Some Data 3
      1 -> 25
      2
      0 -> Some Data 2
      1 -> 78
      3
      0 -> Some Data 1
      1 -> 56

      Apparently *this* is the default behavior. My live data contained only
      two elements, which happened to be in ascending order, thus appeared that
      it was actually sorting them. Mystery solv-ed.

      Interestingly, it doesn't give so much as a Notice.

      --
      Alan Little
      Phorm PHP Form Processor

      Comment

      • Alan Little

        #4
        Re: usort() Behavior?

        Carved in mystic runes upon the very living rock, the last words of Alan
        Little of comp.lang.php make plain:
        [color=blue]
        > Calling usort() with 'TestSort' gives the expected results. Calling it
        > with 'xxx' gives me a reversed array:
        >
        > Apparently *this* is the default behavior. My live data contained only
        > two elements, which happened to be in ascending order[/color]

        Doh! Here's a lesson boys and girls: KISS (Keep It Simple, Stupid).

        The data didn't "happen" to be in ascending order. I re-examined my code
        and realized it would *always* be in ascending order, thus all I have to
        do is call array_reverse() .

        Oh well. Hey, even experienced programmers can waste an hour chasing
        their tail.

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Pedro Graca

          #5
          Re: usort() Behavior?

          Alan Little wrote:[color=blue]
          > Interestingly, it doesn't give so much as a Notice.[/color]

          php$ cat alan.php
          <?php
          error_reporting (E_ALL);
          ini_set('displa y_errors', '1');

          function TestSort($a, $b) {
          if ($a[1] == $b[1]) return 0;
          return ($a[1] > $b[1]) ? -1 : 1;
          }

          /* Here's some sample data: */

          $Data = array (
          array ('Some Data 1', 56),
          array ('Some Data 2', 78),
          array ('Some Data 3', 25),
          array ('Some Data 4', 43)
          );

          /* test 1 */
          $Data1 = $Data;
          usort($Data1, 'TestSort');
          print_r($Data1) ;
          echo "\n\n========\n \n";

          /* test 2 */
          $Data2 = $Data;
          usort($Data2, 'xxx');
          print_r($Data2) ;
          echo "\n\n========\n \n";

          ?>

          php$ php alan.php
          Array
          (
          [0] => Array
          (
          [0] => Some Data 2
          [1] => 78
          )

          [1] => Array
          (
          [0] => Some Data 1
          [1] => 56
          )

          [2] => Array
          (
          [0] => Some Data 4
          [1] => 43
          )

          [3] => Array
          (
          [0] => Some Data 3
          [1] => 25
          )

          )


          ========


          Warning: usort(): Invalid comparison function. in
          /home/pedro/src/php/alan.php on line 21
          Array
          (
          [0] => Array
          (
          [0] => Some Data 1
          [1] => 56
          )

          [1] => Array
          (
          [0] => Some Data 2
          [1] => 78
          )

          [2] => Array
          (
          [0] => Some Data 3
          [1] => 25
          )

          [3] => Array
          (
          [0] => Some Data 4
          [1] => 43
          )

          )


          ========


          php$

          --
          Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
          == ** ## !! !! ## ** ==
          TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
          bypass the spam filter. I will answer all pertinent mails from a valid address.

          Comment

          • Alan Little

            #6
            Re: usort() Behavior?

            Carved in mystic runes upon the very living rock, the last words of Pedro
            Graca of comp.lang.php make plain:
            [color=blue]
            > Alan Little wrote:[color=green]
            >> Interestingly, it doesn't give so much as a Notice.[/color]
            >
            > Warning: usort(): Invalid comparison function. in
            > /home/pedro/src/php/alan.php on line 21[/color]

            Interesting. I even threw in an undefined variable to make sure the error
            reporting was set properly. It gave me a Notice, but nothing from usort().
            Maybe a platform (Win) or version (4.3.6) issue. Oh well, no big deal.

            --
            Alan Little
            Phorm PHP Form Processor

            Comment

            Working...