how to sort array of strings in php ? [here strings are the path. e.g. /root/mandy/a.pdf, b.pdf & c.pdf
how to sort array of strings in php? Here strings are the paths. e.g. /root/mandy/a.pdf, b.pdf & c.pdf etc.
I tried with sort() function with it's parameters, however it's not working ?
EDIT (based on OP's comments):
My bad ! I haven't posted this question correctly. Apologies...
Actually it's an array of maps [not sure may be array of array] -> array("path 开发者_如何学Go=> "file_name") & i need to sort it accordingly the file_name.
Or even better, use strcmp to compare the strings
uasort($yourArray, function ($a, $b) {
return strcmp($a['path'], $b['path']);
});
$strings = array('/root/mandy/c.pdf', '/root/mandy/a.pdf', '/root/mandy/b.pdf');
sort($strings);
print_r($strings);
It works for me.
Based on your updates.
You could try using uasort(), which lets you define a comparison function and also maintains index association (not sure if you need it, use usort() if you don't).
I've written the comparison function here as a closure, which is available since PHP 5.3. In earlier versions, you can create a normal function and use its name as a string in the second parameter of uasort().
uasort($yourArray, function ($a, $b) {
if ($a['path']==$b['path']) return 0;
return $a['path']>$b['path'] ? 1 : -1;
});
You can change path to anything that matches the key of your filenames, I just made an assumption based on your comment.
加载中,请稍侯......
精彩评论