Unexpected results using ksort on arrays in which the keys are mixed alphanumeric strings.
| From: | Clancy | Date: | Tue, 24 Feb 2009 04:33:15 +0000 |
| Subject: | Unexpected results using ksort on arrays in which the keys are mixed alphanumeric strings. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-288723@lists.php.net to get a copy of this message | ||
I have been experimenting using four character alphanumeric keys on an array, and when I
generated a random set of keys, and then used ksort to sort the array, I was very
surprised to find that if the key contained any non-numeric character, or if it started
with zero, the key was sorted as a base 36 number (0- 9, A-Z, as I expected. However if
the key only contained numbers, and did not start with zero, it was sorted to the end of
the list.
Thus:
0000
0009
000A
0999
09A0
ASDF
ZZZZ
1000
9999
I presume this is related to last weeks discussions about casting variables, but I cannot
understand why 0999 should go to the start of the list, while 1000 goes to the end. Can
anyone explain this logically?