php.net/sort
Description
bool sort ( array &array [, int sort_flags] )This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
[snip]
Returns TRUE on success or FALSE on failure.
How do you fail to sort an array? You can sort it correctly, or you can sort it incorrectly. But the actual sorting doesn’t fail…
update: see comments. DJ and Scott win, and PHP and I lose.
September 7th, 2006 at 2:02 pm
you fail sorting an array if the array has recursive references in it…
try sorting $GLOBALS. It contains within it a reference to itself
serializing it also fails.
also, you may or may not be able to sort objects, images, and other resource types… I haven’t tried though.
September 7th, 2006 at 2:05 pm
Even if they aren’t defining sort failure as failing to sort correctly (which to me, is a failure condition) there’s plenty of things that can go wrong in a sort.
*max recursion depth on a recursive sorting algorithm
*not enough memory for an entire copy of the array (for algorithms that don’t work in place)
*any of the functions you depend on to sort return false (php is great at having things that can return false in a “WTF” moment. Like, you, when you segfault *in* malloc().
September 7th, 2006 at 11:03 pm
Okay, you guys are both pretty much right. I didn’t think it through fully. I stand corrected. =]
Ben suggested that if the array’s contents are not comparable to each other, sort might return false, indicating the array is not sortable. Which makes a lot of sense. I tried to sort an array of things not comparable to each other: a string, an int, and an array. sort() returns true, and does nothing. Which is probably the worst thing to do. Boo PHP.