sort a custom field base on the date or anything

  1. Multiline is not an array (rant)
  2. You need to use standard dates for this to work DD/MM/YYYY (first row)
  3. Use the PHP function usort() to sort the fields
  4. Use strtotime() to convert that date to a timestamp and date_i18n() to output it

If you need an actual example, change the date formats to what I’ve shown you above, update the screenshot and I’ll give you the code. First line must be DD/MM/YYYY.

THE CODE

// Demo set (replace with you custom fields)
$courses = array(
    '27/10/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
    '19/10/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
    '17/01/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
);
// Convert the courses to objects and translate the dates
$sorted_courses = array(); // Holds objects for sorting
foreach($courses as $course){
    $course = nl2br($course);
    // Replace <br /> with \r\n
    $course = preg_replace('~<br[\s]*/[\s]*>~i', PHP_EOL, $course);
    // Split by \r\n and trim each entry
    list($date, $city, $link) = array_map('trim', preg_split('~[\r\n]+~', $course));
    // Validate the date format [ :) ]
    if(!preg_match('~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~', $date, $slices)) continue;
    // Extract date elenents
    list($match, $day, $month, $year) = $slices;
    // And now create the object
    $sorted_courses[] = (object)array(
        'time'  => $time = mktime(1, 1, 1, $month, $day, $year),
        'date'  => $date = date_i18n('d F Y', $time),
        'city'  => $city,
        'link'  => $link,
    );
}
// Sort elements ascending (using callback)
usort($sorted_courses, function($item1, $item2){
    // Compare the timestamps and sort ascending
    return $item1->time > $item2->time;
});
// Got through the sorted courses and display them
foreach($sorted_courses as $sorted_course){
    echo "<ul>";
        echo "<li>{$sorted_course->date}</li>";
        echo "<li>{$sorted_course->city}</li>";
        echo "<li><a href="https://wordpress.stackexchange.com/questions/32652/{$sorted_course->link}">Inschrijven</a></li>";
    echo "</ul>";
}