本文整理汇总了PHP中CMap::compare方法的典型用法代码示例。如果您正苦于以下问题:PHP CMap::compare方法的具体用法?PHP CMap::compare怎么用?PHP CMap::compare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CMap
的用法示例。
在下文中一共展示了CMap::compare方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: compare
/**
* Determines the order in which two maps should appear in a place where it matters.
*
* You can use your own comparator for the comparison of the values in the maps, but the default comparator has got
* you covered when comparing scalar values, such as `string`, `int`, `float`, and `bool` in the ascending order or
* in the descending order if you use `CComparator::ORDER_DESC`. And the default comparator is smart enough to know
* how to compare objects of those classes that conform to the IEqualityAndOrder interface (static or not),
* including CUStringObject, CArrayObject, CMapObject, CTime etc. See the [CComparator](CComparator.html) class for
* more on this.
*
* @param map $toMap The second map for comparison.
* @param callable $comparator **OPTIONAL. Default is** `CComparator::ORDER_ASC`. The function or method to be
* used for the comparison of any two values. If this parameter is provided, the comparator should take two
* parameters, with the first parameter being a value from *this* map and the second parameter being a value from
* the second map, and return `-1` if the value from *this* map would need to go before the value from the second
* map if the two were being ordered in separate, `1` if the other way around, and `0` if the two values are equal.
*
* @return int A negative value (typically `-1`) if *this* map should go before the second map, a positive value
* (typically `1`) if the other way around, and `0` if the two maps are equal.
*
* @link CComparator.html CComparator
*/
public function compare($toMap, $comparator = CComparator::ORDER_ASC)
{
return CMap::compare($this->m_map, $toMap, $comparator);
}
示例2: testCompare
public function testCompare()
{
// Using the default comparator.
$map0 = ["one" => "a", "two" => "b", "three" => "c"];
$map1 = ["one" => "a", "two" => "b", "three" => "c"];
$this->assertTrue(CMap::compare($map0, $map1) == 0);
$map0 = ["a" => "a", "two" => "b", "three" => "c"];
$map1 = ["one" => "a", "two" => "b", "three" => "c"];
$this->assertTrue(CMap::compare($map0, $map1) < 0);
$map0 = ["one" => "a", "two" => "b", "three" => "c"];
$map1 = ["one" => "d", "two" => "e", "three" => "f"];
$this->assertTrue(CMap::compare($map0, $map1) < 0);
$map0 = ["one" => "d", "two" => "e", "three" => "f"];
$map1 = ["one" => "a", "two" => "e", "three" => "f"];
$this->assertTrue(CMap::compare($map0, $map1) > 0);
$map0 = ["one" => "a", "two" => "b"];
$map1 = ["one" => "a", "two" => "b", "three" => "c"];
$this->assertTrue(CMap::compare($map0, $map1) < 0);
$map0 = ["one" => 1, "two" => 2, "three" => 3];
$map1 = ["one" => 1, "two" => 2, "three" => 3];
$this->assertTrue(CMap::compare($map0, $map1) == 0);
$map0 = [1, 2, 3];
$map1 = [1, 2, 3];
$this->assertTrue(CMap::compare($map0, $map1) == 0);
$map0 = [1, 2, 3];
$map1 = [4, 5, 6];
$this->assertTrue(CMap::compare($map0, $map1) < 0);
$map0 = [4, 5, 6];
$map1 = [3, 5, 6];
$this->assertTrue(CMap::compare($map0, $map1) > 0);
$map0 = [1.2, 3.4, 5.6];
$map1 = [1.2, 3.4, 5.6];
$this->assertTrue(CMap::compare($map0, $map1) == 0);
$map0 = ["one" => "a", "two" => "b", "three" => "c"];
$map1 = ["one" => u("a"), "two" => u("b"), "three" => u("c")];
$this->assertTrue(CMap::compare($map0, $map1) == 0);
// Using a custom comparator.
$map0 = ["one" => "a", "two" => "b", "three" => "c"];
$map1 = ["one" => "A", "two" => "B", "three" => "C"];
$comparator = function ($string0, $string1) {
return CString::toLowerCase($string0) === CString::toLowerCase($string1) ? 0 : -1;
};
$this->assertTrue(CMap::compare($map0, $map1, $comparator) == 0);
}