Re: Unexpected results using ksort on arrays in which the keys are mixed alphanumeric strings.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 24 Feb 2009 10:25:25 +0100, jochem@xxxxxxxxxxxxx (Jochem Maas) wrote:

>Clancy schreef:
>> 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.
>
>did your experiment include reading the manual? or did you expect ksort() to known what
>kind of sort you wanted? ... try specifying a sort flag:
>
I have to plead guilty to your first question, but what I really wanted to know was how
the sort procedure could decide to treat any key starting with zero as a string, but then
decide that any wholly numeric key, not starting with zero, was greater than ZZZZ.

But your second question served a useful purpose. I had been thinking that I ought to sort
the keys of my index array to speed up the process of fetching a specific entry.
Presumably PHP has to do some form of sort to find the key you have specified, but if you
don't know (and can't specify) how it orders the keys there is no point to trying to sort
them for it. Then I realised that PHP must have its own highly sophisticated indexing
system, and it probably wouldn't make the slightest difference whether or not the keys
were sorted.

So I made several different dummy index files with random keys, some sorted and some
unsorted, and timed how long it took to see if every key on one file was set in one of the
other files. This showed first that it didn't matter whether or not the keys in the second
file were sorted, and, more importantly, that the 'if(isset())' process is so fast that in
any situation I can think of you can completely ignore it. Testing all the keys of one
100,000 entry array against another 100,000 entry array took only 300 milliseconds, or
about 3 µs per test.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux