Custom Sort Order with SimplePie

Deadly 提交于 2019-12-24 03:45:09

问题


Using SimplePie, how can I sort items by random order and title (alphabetic)? According to this page (http://simplepie.org/wiki/tutorial/sorting_by_custom_criteria_instead_of_date), the documentation says to extend the class and write your own method. But without some more examples, I don't seem to be able to code it by myself.

Currently I take all the feed items and put them into arrays and sort them. However, the default sort order of SimplePie is by date. Thus, if some urls have newer items let's say 20 of them are new and if I fetch only 20 items overall, then the output is occupied with the items from the limited source of feed urls although I want to show items in random order picking up a few from each.

I'm hoping some more working examples with the extended class method.


回答1:


Actually it was really simple. haha

class SimplePie_Title_Sort extends SimplePie
{
    public static function sort_items($a, $b)
    {
        return strcmp($a->get_title(),$b->get_title());
    }
}
class SimplePie_Random_Sort extends SimplePie
{
    public static function sort_items($a, $b)
    {
        return rand(-1, 1);
    }
}


来源:https://stackoverflow.com/questions/12127709/custom-sort-order-with-simplepie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!