Searched but could not find anything I thought was useful to help.
I have an array of file names. Those names start with either a number (3 digit) or alphabetic characters. Now, I want the alpha’s first with the numeric last. I can’t figure it out.
Thanks
E
What have you tried so far?
There are many ways of doing it. The quickest is probably just sorting the array normally and then repeat checking the first item for an integer starting character, removing it and adding it to the end.
Something like:
function AlphaFirst($Array){
if(count($Array) > 0){
sort($Array);
while(is_numeric(substr($Array[0], 0, 1))){
$Array[] = array_shift($Array);
}
}
return $Array;
}
$ToSort = array('fileA', '1fileA', '4fileA', 'fileB');
$Sorted = AlphaFirst($ToSort);
var_dump($ToSort, $Sorted);
It might be more efficient to use one of the many sorting capabilities that PHP has to offer, but I don’t think it would be. For reference, check usort out.
asort, natsort, uksort. All have the numbers first and alpha last. I want alpha first. Oh, and I put the leading character of the alpha as an underscore (does put the sort at the beginning of the alphas but…)
P.S. Data is like
001.jpg
005.jpg
047.jpg
picture1.jpg
005.jpg
another picture1.jpg
etc…
Jake,
I used your “example” and it works as I want it. Thanks.
E