/** * Sorts posts by date (descending) or by * chapter (ascending). Assigns post.next and post.prev * * @return void */ private function sortPosts() { $this->app->writeln("\n<comment>Sorting</comment>"); $cmpFn = function (Content $one, Content $other) { // Sort by chapters if ($one instanceof Doc && $other instanceof Doc) { if ($one->chapter == $other->chapter) { return 0; } return $one->chapter < $other->chapter ? -1 : 1; } // Sort by date if ($one->date == $other->date) { return 0; } return $one->date > $other->date ? -1 : 1; }; foreach ($this->site->categories as $cat => &$posts) { // Sort posts usort($posts, $cmpFn); // Assign next and previous post within the category foreach ($posts as $key => $post) { if (isset($posts[$key - 1])) { $post->next = $posts[$key - 1]; } if (isset($posts[$key + 1])) { $post->prev = $posts[$key + 1]; } } } $this->app->writeln("Done!"); }