PHP Custom array sort filenames by year and month, newest first -


i've got array of files looks this:

array (   0 => 'scpt-01-2010.phtml',   1 => 'scpt-01-2011.phtml',   2 => 'scpt-02-2010.phtml',   3 => 'scpt-02-2011.phtml',   4 => 'scpt-03-2010.phtml',   5 => 'scpt-04-2010.phtml',   6 => 'scpt-05-2010.phtml',   7 => 'scpt-06-2010.phtml',   8 => 'scpt-07-2010.phtml',   9 => 'scpt-08-2010.phtml',   10 => 'scpt-09-2010.phtml',   11 => 'scpt-10-2010.phtml',   12 => 'scpt-11-2010.phtml',   13 => 'scpt-12-2010.phtml', ); 

how can sort 2011 files appear first, in order of month (so should lead scpt-02-2011.phtml)?

i've tried main sorting functions natsort, rsort, arsort etc i'm not getting anywhere fast!

thanks in advance.

function customsort($a, $b) {     // extract values simple regular expression     preg_match('/scpt-(\d{2})-(\d{4})\.phtml/i', $a, $matches1);     preg_match('/scpt-(\d{2})-(\d{4})\.phtml/i', $b, $matches2);      // if years equal, compare month     if ($matches2[2] == $matches1[2]) {         return $matches2[1] - $matches1[1];     // otherwise, compare year     } else {         return $matches2[2] - $matches1[2];     } }  // sort array usort($array, 'customsort'); 

this method uses usort() sort array, passing name of our comparison function.

http://php.net/usort


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -