Jump to content

sort array in item order when numbers go over 10


jasonc

Recommended Posts

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>

 

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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.