Portal Home > Knowledgebase > Articles Database > array file sorting


array file sorting




Posted by blueseraph, 09-03-2007, 01:04 PM
need some help with a script to sort images for a gallery. I have the thumbnails in a dir and need to display them in the order that you would see them in windows. here is my code: 'gets all thubnails from the dir' $dirtoopen="artist/".$galName; $tn=0; if ($handle = opendir($dirtoopen)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $tn++; $tNF[$tn]=$file; } } closedir($handle); The order that they are retrieved in is not the same order that I view them in ftp or within windows. I have added sort($tNF); after the closedir($handle); but the orde3r still isn't right. With the sort it displayes numbers (070706a) then jumps to letters (Untitled-03-09-2007) then underscores (_63) and finally back to letters (a15 ) here is a list of how it is outputing the sorted array. 3d1, 3d2, 3d3small, 3d4, Untitled-111, Untitled-1a, Untitled-1b, Untitled-1f, Untitled-1nnn , Untitled-38-copya, Untitled-38, Untitled-4-copya-redo, Untitled-51, Untitled-52, Untitled-54, Untitled-55, art5a, art7, cs5, ff02, ff03, ff04, ff05, ff06, hane-1, k1, k2 Please help me figure out how to sort them (_,00-99,aA-zZ)

Posted by blueseraph, 09-03-2007, 01:22 PM
just to be clearer. This is what is currently outputing: Untitled-05, Untitled-1cs3a-better, Untitled-38, Untitled-41, _6A, art12-copy, cs4, fractal-apple, hane-1, objects And this is what it should be: _6A, art12-copy, cs4, fractal-apple, hane-1, objects, Untitled-1cs3a-better, Untitled-05, Untitled-38, Untitled-41,

Posted by debiannerd, 09-03-2007, 05:56 PM
ohh it is normal.... see for the sort command ... U goes first... it makes a difference between uppercase letter and lowercase so for example if you would sort U-b-a-A it will display A-U-a-b Uppercases go first before lowercase! got it? the other function I know which would be case insensitive is natcasesort() however this would put all underscore "_file" at the bottom... good luck... Ali

Posted by csparks, 09-03-2007, 06:29 PM
have you tried: I have never tried it before, but may work better then sort()

Posted by debiannerd, 09-03-2007, 06:32 PM
No no... still will be same "case insensitive" (puts UpperCase letters before lowercase) come one guys... check out the doc before posting!

Posted by mwatkins, 09-03-2007, 08:20 PM
In python you have several solutions, the easiest of which I've shown: # your list of randomly unsorted file names unsorted = ['hane-1', 'Untitled-38', '_6A', 'Untitled-41', 'cs4', 'fractal-apple', 'Untitled-05', 'objects', 'art12-copy', 'Untitled-1cs3a-better'] # return a (new) sorted list print sorted(unsorted, key=str.lower) # the result: ['_6A', 'art12-copy', 'cs4', 'fractal-apple', 'hane-1', 'objects', 'Untitled-05', 'Untitled-1cs3a-better', 'Untitled-38', 'Untitled-41']

Posted by blueseraph, 09-03-2007, 10:24 PM
does anyone know how to use the usort function i think that might work but i have no idea how i would creat the user defined comparison.

Posted by Bangalore Job Mob, 09-03-2007, 10:35 PM
That'd almost be a useful post if this was Python code.

Posted by mwatkins, 09-04-2007, 05:02 AM
I can read. I frequently post Python implementations in threads for other languages as a way of exposing people to new things and ideas. Not every post from me is as such; I go out of my way to answer enough questions with directly relevant information. You are new here. Perhaps read a little before commenting. Back to the OP - my example should suggest a course of action for your PHP implementation. There are a number of ways you could go about implementing a similar approach; one might be to create an associative array with a lower case filename as the "key", regular case strings as the value; sort the keys, and then loop through that sorted list, returning the values from your key/value array. Or, simpler yet, the OP could just read the PHP docs and eventually be led to uksort, which almost does the job, and then perhaps to array_flip(), which, in conjunction with uksort, certainly can do the job: Output: Use array_keys($flipped) in your app's loop and you are done. (Note that the user provided comparison function is something akin to my one line Python example which is, incidentally, made easier/possible because Python has the list data type, not just associative arrays.)

Posted by Edward_K, 09-04-2007, 05:07 AM
If you can deal with the underscores or handle them manually, it's close.

Posted by mwatkins, 09-04-2007, 05:10 AM
uksort($flipped, "strcasecmp") deals with the underscores. What's bizzare is why natcasesort does not do the same, by default. There may perhaps be a LOCALE setting one can set to affect that. Last edited by mwatkins; 09-04-2007 at 05:13 AM.

Posted by Bangalore Job Mob, 09-04-2007, 12:46 PM
Are you sure you aren't more interested in exposing people to your raging ego? You're right, only 200 of your posts are as such. You claim that Python makes you more productive, yet all it seems to have enabled you to do is talk about Python. Incessantly.

Posted by mwatkins, 09-04-2007, 02:23 PM
Since the bulk of my posts are in technical & security, or programming discussion threads, its not surprising that Python would come up in my writing fairly often since development is frequently the topic matter. If your count has any basis in reality, 200 posts out of 1,552 - over a span of 6 years - wouldn't be an excessive ratio at all. Lets see if your signal to noise ratio stands up over time. Whether or not my postings are useful to anyone is up to the reader to decide. Whether posts from anyone have worth frequently comes down to intent; clearly my intent is to be helpful even when I do a little education and evangelize along the way.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Servermatrix down?? (Views: 658)