Jump to content

Sorting Array


drisate

Recommended Posts

hey guys!

i have an arrays that looks like this:

 

Array
(
[0] => Array
(
[name] => backup_fghj.sql.gz
[size] => 10056579
[perm] => -rw-r--r--
[type] => file
[time] => 22 February 2012 18:53:16
[stamp] => 85745
)

[1] => Array
(
[name] => backup_sdfg.sql.gz
[size] => 13755947
[perm] => -rw-r--r--
[type] => file
[time] => 16 November 2012 14:26:13
[stamp] => 25745
)

[2] => Array
(
[name] => backup_dfgh.sql.gz
[size] => 11936839
[perm] => -rw-r--r--
[type] => file
[time] => 15 September 2012 22:18:55
[stamp] => 84574
)

[3] => Array
(
[name] => backup_erty.sql.gz
[size] => 11009205
[perm] => -rw-r--r--
[type] => file
[time] => 22 July 2012 09:08:16
[stamp] => 34574
)

)

 

 

I need to sort it by stamp desc ... how can i do that?

 

 $file = dir_list("../forum/CPadmin/backups");

 $f=0;
 foreach($file as $fichier){
	 if (strstr($file[$f]['name'], '.gz')){
		 $page_content .= '<a href="/forum/CPadmin/backups/'.$file[$f]['name'].'">'.$file[$f]['time'].'</a></br>';
	 }
 $f++;

Link to comment
https://forums.phpfreaks.com/topic/270792-sorting-array/
Share on other sites

for a DESC numeric sort the function just need to be

 

function customSort( $a, $b )
{
return $b['stamp'] - $a['stamp'];
}

 

Absolutely. I tend to use the initial check to see if the values are different (as in my example) because it let's me easily add additional sorting criteria.So, if the 'stamp' values were the same there could be some additional logic to then sort on field#2, then field#3, etc.

 

So, using your more elegant solution . . .

function customSort($a, $ B)
{
   if($a['stamp'] != $b['stamp']) { return $b['stamp'] - $a['stamp']; }
   if($a['field2'] != $b['field2']) { return $b['field2'] - $a['field2']; }
   if($a['field3'] != $b['field3']) { return $b['field3'] - $a['field3']; }
   return 0;
}

Link to comment
https://forums.phpfreaks.com/topic/270792-sorting-array/#findComment-1393053
Share on other sites

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.