string comparison - php natural comparsion sort that ignores (but does not get rid of) non-numeric data -


basically, have directory bunch of files names have loaded array. file names tell me text represent (i.e. prologue, chapterone, chaptertwo), in file name include sequential number keep them ordered. 'prollecture1.xml', 'prollecture2.xml', 'prollecture3.xml', . . . 'prollecture12.xml', 'chapteronelecture13.xml', 'chapteronelecture14.xml'. . . 'conclusionlecture18.xml', etc.

i want sort array lists them in numerical order. using "natural comparison sort" gets me close, sort begins first character of file name, , 'chapteronelecture13.xml' listed before 'prollecture1.xml' because 'c' comes before 'p'. if had known wanted beginning have put numbers first. change file names lot of work.

my question: there way "natural string comparison" ignore first part of file name , begin @ "lecture##"? or better, can sort ignore (but not remove) non-numeric data , sort array solely numbers embedded in file name?

thanks help.

i think there's no built-in function this, using usort may accomplish that:

function just_numerical_sort($a, $b) {     return preg_replace('/[^0-9]/', '', $a) - preg_replace('/[^0-9]/', '', $b); }  usort($array, 'just_numerical_sort'); 

the preg_replace returns copy of $a or $b non-numerical characters removed (i haven't tested it, think works).


Comments