Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 => "file_name") & i need to sort it accordingly the file_name.

like image 520
Mandar Pande Avatar asked May 16 '11 07:05

Mandar Pande


Video Answer


2 Answers

$strings = array('/root/mandy/c.pdf', '/root/mandy/a.pdf', '/root/mandy/b.pdf');
sort($strings);
print_r($strings);

It works for me.

like image 64
yitwail Avatar answered Oct 27 '22 01:10

yitwail


Or even better, use strcmp to compare the strings

uasort($yourArray, function ($a, $b) {
    return strcmp($a['path'], $b['path']);
});
like image 22
radu_paun Avatar answered Oct 26 '22 23:10

radu_paun