jasonc Posted November 6, 2009 Share Posted November 6, 2009 how do i sort the following file examples so they are in number order? abc - 23-11-2009 - Agenda Item 1 - Name of item 1.pdf abc - 23-11-2009 - Agenda Item 2 - Name of item 2.pdf abc - 23-11-2009 - Agenda Item 3 - Name of item 3.pdf abc - 23-11-2009 - Agenda Item 4 - Name of item 4.pdf abc - 23-11-2009 - Agenda Item 5 - Name of item 5.pdf abc - 23-11-2009 - Agenda Item 6 - Name of item 6.pdf abc - 23-11-2009 - Agenda Item 7 - Name of item 7.pdf abc - 23-11-2009 - Agenda Item 8 - Name of item 8.pdf abc - 23-11-2009 - Agenda Item 9 - Name of item 9.pdf abc - 23-11-2009 - Agenda Item 10 - Name of item 10.pdf abc - 23-11-2009 - Agenda Item 11 - Name of item 11.pdf abc - 23-11-2009 - Agenda Item 12 - Name of item 12.pdf as it is showing as abc - 23-11-2009 - Agenda Item 1 - Name of item 1.pdf abc - 23-11-2009 - Agenda Item 10 - Name of item 10.pdf abc - 23-11-2009 - Agenda Item 11 - Name of item 11.pdf abc - 23-11-2009 - Agenda Item 12 - Name of item 12.pdf abc - 23-11-2009 - Agenda Item 2 - Name of item 2.pdf abc - 23-11-2009 - Agenda Item 3 - Name of item 3.pdf abc - 23-11-2009 - Agenda Item 4 - Name of item 4.pdf abc - 23-11-2009 - Agenda Item 5 - Name of item 5.pdf abc - 23-11-2009 - Agenda Item 6 - Name of item 6.pdf abc - 23-11-2009 - Agenda Item 7 - Name of item 7.pdf abc - 23-11-2009 - Agenda Item 8 - Name of item 8.pdf abc - 23-11-2009 - Agenda Item 9 - Name of item 9.pdf <? $files = glob('agendas/*Agenda Item*.pdf'); $sortfile = array(); foreach($files as $file){ //Extract date if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) { //add date to array (file as key) $sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]); } } arsort($sortfile); //Sort an array (the dates) and maintain index association (file names) $sortfile = array_keys($sortfile); $datestring = strtotime(basename(substr($sortfile[0], 14, 10),".pdf")); $anchortext = date('j M Y',$datestring); $url = date('dmY',$datestring); ?><li><a href="?a=<?=$url;?>"><? if (strlen(date('j',$datestring)) == 1) { ?> <? } ?><strong><?=$anchortext;?></strong></a></li> Link to comment https://forums.phpfreaks.com/topic/180583-sort-array-in-item-order-when-numbers-go-over-10/ Share on other sites More sharing options...
lemmin Posted November 6, 2009 Share Posted November 6, 2009 You would have to put leading zeros in front of the sorting number. Link to comment https://forums.phpfreaks.com/topic/180583-sort-array-in-item-order-when-numbers-go-over-10/#findComment-952716 Share on other sites More sharing options...
JAY6390 Posted November 6, 2009 Share Posted November 6, 2009 Try using this function http://www.php.net/manual/en/function.natsort.php Link to comment https://forums.phpfreaks.com/topic/180583-sort-array-in-item-order-when-numbers-go-over-10/#findComment-952717 Share on other sites More sharing options...
jasonc Posted November 6, 2009 Author Share Posted November 6, 2009 Try using this function http://www.php.net/manual/en/function.natsort.php i used the natsort but this did not sort the file names into order. this my code with natsort.. <? $files = glob('agendas/*Agenda Item*.pdf'); $sortfile = array(); foreach($files as $file){ //Extract date if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) { //add date to array (file as key) $sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]); } } natsort($sortfile); //Sort an array (the dates) and maintain index association (file names) $sortfile = array_keys($sortfile); foreach ($files as $key=>$value) { $datestring = strtotime(basename(substr($sortfile[$key], 14, 10),".pdf")); // $anchortext = date('j M Y',$datestring); $itemnumber = basename(substr($sortfile[$key], 39, 2),".pdf"); $itemname = basename(substr($sortfile[$key], 43),".pdf"); $url = date('dmY',$datestring); ?><li><a href="?a=<?=$url;?>"><? if (strlen(date('j',$datestring)) == 1) { ?> <? } ?><strong><?=$itemnumber."=".$itemname;?></strong></a><? if (strlen(date('j',$datestring)) == 1) { ?> <? } } ?></li> Link to comment https://forums.phpfreaks.com/topic/180583-sort-array-in-item-order-when-numbers-go-over-10/#findComment-952782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.