本文整理匯總了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);
}