473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
http://www.phorm.com/
Jul 17 '05 #1
5 2949
Alan Little wrote:
[edited]
I wrote a sorting function to use with usort(). [...] changed the name
of the sorting function, but forgot to update it in the usort() call.
Yet, it still was working. [...] I changed the name of the callback function in the usort() call
to random letters, and it still worked.


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

error_reporting(E_ALL);
ini_set('display_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.
Jul 17 '05 #2
Carved in mystic runes upon the very living rock, the last words of Pedro
Graca of comp.lang.php make plain:
Alan Little wrote:
[edited]
I wrote a sorting function to use with usort().

[...]
changed the name
of the sorting function, but forgot to update it in the usort() call.
Yet, it still was working.

[...]
I changed the name of the callback function in the usort() call
to random letters, and it still worked.


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

Additionally, insert

error_reporting(E_ALL);
ini_set('display_errors', '1');

at the top of your code.


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
http://www.phorm.com/
Jul 17 '05 #3
Carved in mystic runes upon the very living rock, the last words of Alan
Little of comp.lang.php make plain:
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


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
http://www.phorm.com/
Jul 17 '05 #4
Alan Little wrote:
Interestingly, it doesn't give so much as a Notice.


php$ cat alan.php
<?php
error_reporting(E_ALL);
ini_set('display_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.
Jul 17 '05 #5
Carved in mystic runes upon the very living rock, the last words of Pedro
Graca of comp.lang.php make plain:
Alan Little wrote:
Interestingly, it doesn't give so much as a Notice.


Warning: usort(): Invalid comparison function. in
/home/pedro/src/php/alan.php on line 21


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
http://www.phorm.com/
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
I am having to sort an array of objects by an arbitrary object field name that you are not going to know in advance, therefore, I cannot name the sorting function accordingly nor reference the...
0
by: Spidah | last post by:
I am having trouble using the usort function inside a class. Is this possible and if so what is the correct way to pass the compare function to it? I assume this function must also be inside the...
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
3
by: youngord | last post by:
<?php $mix=array( array("A",10), array("B",5), array("C",100) ); function com($x,$y){ echo $x; } usort($mix,'com');
1
by: ojsimon | last post by:
Hi I Am using Simplepie to display some rss feeds, currently it displays them in date and time order but i want it to display them in relevance order by using a usort function. here is the code...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.