+1  A: 

You'll first want to split the string into lines, split it into sub-arrays, run through usort, and then join it all back together. For example:

function lineSplit( &$item )
{
    $item = explode( '|', $item );
}

function lineSort( $item1, $item2 )
{
    return strcmp( $item1[ 3 ], $item2[ 3 ] );
}

function lineJoin( &$item )
{
    $item = join( '|', $item );
}

$str = '...';

// First split on the comma to get each line.
$lines = explode( ",\n", $str );

// Now split each line into subarrays
array_walk( $lines, 'lineSplit' );

// Perform the sort using a user-defined function.
usort( $lines, 'lineSort' );

// Now join the subarrays into strings.
array_walk( $lines, 'lineJoin' );

// And finally merge the lines again.
$str = join( ",\n", $lines );
Jon Benedicto
A: 
<?php
$string = "event name|event description|event type|2009-08-01|event time|event details,
event name|event description|event type|2009-08-02|event time|event details,
event name|event description|event type|2009-08-01|event time|event details,
event name|event description|event type|2009-08-03|event time|event details,";
   $arr = array();
   $strs = explode(',', $string);
   print_r($strs);
   foreach ($strs as $i => $str)
   {
      if (empty($str)) continue;

      $expl = explode('|', $str);
      $arr[strtotime($expl[3]) . $i] = $str;
   }
   ksort($arr);
   $result = implode(",\n", $arr);
   print_r($result);
?>

output:

event name|event description|event type|2009-08-01|event time|event details,
event name|event description|event type|2009-08-01|event time|event details,
event name|event description|event type|2009-08-02|event time|event details,
event name|event description|event type|2009-08-03|event time|event details
inakiabt
only one loop :)
inakiabt
Thanks so much! This works perfectly for me. I needed to add a tailing comma to the end of $result = implode(",\n", $arr); like this $result = implode(",\n", $arr) . ','; Other then that, perfect.
I'm glad you like it
inakiabt
+2  A: 
function sortStringByDate($str)
{
 $arr = explode(",\n", $str);
 foreach ($arr as $key => $val)
 {
  $arr[$key] = explode('|', $val);
 }
 $new_arr = array();
 foreach ($arr as $i => $vals)
 {
  $time = strtotime($vals[3].' '.$vals[4]);
  $new_arr[$time] = $vals;
 }
 ksort($new_arr);

 foreach ($new_arr as $key => $value)
 {
  $almost[] = implode('|', $value);
 }

 return implode(",\n", $almost);
}
James Skidmore
A: 
$arr = explode(',', $inputString);
$map = array()
foreach ($arr as $line){
    $lineArray = explode('|', $line);
    // convert date value to numerical representation of date+time
    $lineArray[3] = ...
    $map[] = $lineArray;
}

array_multisort($map[, ...]);

$result = array()
foreach($map as $lineArray){
    $result[] = implode('|', $lineArray);
}

// the resulting string:
implode(',', $result)
lbp
Careful, this won't work if any of the fields contain commas.
James Skidmore