本文整理汇总了PHP中ObjectCollection::sort方法的典型用法代码示例。如果您正苦于以下问题:PHP ObjectCollection::sort方法的具体用法?PHP ObjectCollection::sort怎么用?PHP ObjectCollection::sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectCollection
的用法示例。
在下文中一共展示了ObjectCollection::sort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: array
<?php
namespace DesignPatterns\Strategy;
/**
* strategy pattern
*
* Terminology:
* - Context
* - Strategy
* - Concrete Strategy
*
* Purpose:
* to separate strategies and to enable fast switching between them.
* also this pattern is a good alternative to inheritance (instead of having an abstract class that is extended)
*
* Examples:
* - sorting a list of objects, one strategy by date, the other by id
* - simplify unit testing: e.g. switching between file and in-memory storage
*
*/
$elements = array(array('id' => 2, 'date' => '2011-01-01'), array('id' => 1, 'date' => '2011-02-01'));
$collection = new ObjectCollection($elements);
$collection->setComparator(new IdComparator());
$collection->sort();
$collection->setComparator(new DateComparator());
$collection->sort();