Esempio n. 1
0
 /**
  * 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);
 }
Esempio n. 2
0
 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);
 }