Re: sorting array by keys?

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

 




On Oct 20, 2015, at 11:59 PM, Tolga wrote:

21.10.2015 09:50 tarihinde Jeffry Killen yazdı:
Hello;

I have been consulting the manual and playing with a few different approaches to this;

this comes the closest to what I want:

function sortAlg($_a, $_b)
                 {
                  return strcasecmp($_a, $_b);
                 };
        uksort($_result['dirs'], 'sortAlg');



$_result['dirs'] is itself an array where the associative string keys are what I want to sort by.

Everything works fine, accept
I have a series of directories that follow the naming pattern 'lab_#'
so, what happens with the sort is that the labs come out
lab_0
lab_1
lab_10
... etc... up to lab_13
then
lab_2
lab_3
... etc... up to lab_9

Am I going to have to code to find these cases and sort them as a separate array by keys?

This is also coming out of Apache on a FreeBSD system. The sorting order appears to be creation date, as the last items created in this part of the file system appear last without any
sorting.

Without any sorting most of the lab_# names come out in the right order, but that is, I believe, because all of these directories and files were posted via ftp transfer at the same time.

So, lab_0 through lab_8 come out in the right order, but lab_9 through lab_13 seem to
appear according to subsequent creation date.



its probably because of sorting algorithms checks char to char, first orders 1st char, then second, then .... , not by their numeric values.

You first need to sort as you are doing now, then collect all the names with same starting letters into new array, (for your example, collect all names with starting 'lab_' into new array) then compare/sort new arrays with using filter_var($str, FILTER_SANITIZE_NUMBER_INT) therefore, you can sort names with numeric values on last step of sorting.

Thank you for your response:
In the interum, I found what is now working:
 uksort($_result['dirs'], 'strnatcmp');
JK
--
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