Question

How do I sort an array like this, by the timestamp field, using Silex and/or Twig? I've looked at the documentation, silex doesnt seem to show anything, and twig is scant http://twig.sensiolabs.org/doc/filters/sort.html.

array (size=10)
  0 => 
    array (size=3)
      'title' => string 'aaaaaa' (length=39)
      'content' => string 'zzzzzzzzzzzzzzzzz'... (length=3324)
      'timestamp' => string '2014-03-18 15:27:13' (length=19)
  1 => 
    array (size=3)
      'title' => string 'aaaaaaaaaa' (length=45)
      'content' => string 'zzzzzzzzzzzz'... (length=2895)
      'timestamp' => string '2014-02-02 12:27:13' (length=19)
  2 => 
    array (size=3)
      'title' => string 'aaaaaaaa' (length=26)
      'content' => string 'zzzzzzzzzzzzzz'... (length=2753)
      'timestamp' => string '2013-12-01 11:45:19' (length=19)

....
Was it helpful?

Solution

What about using some PHP?

usort($data, function($a, $b) {
    return strtotime($a['timestamp']) < strtotime($b['timestamp']) ? -1 : 1;
});

If you are getting the data from a database then you should let the database sort it for you. It is a lot faster than doing it yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top