Example #1
0
 public function testCompare()
 {
     // Using the default comparator.
     $array0 = CArray::fromElements("a", "b", "c");
     $array1 = CArray::fromElements("a", "b", "c");
     $this->assertTrue(CArray::compare($array0, $array1) == 0);
     $array0 = CArray::fromElements("a", "b", "c");
     $array1 = CArray::fromElements("d", "e", "f");
     $this->assertTrue(CArray::compare($array0, $array1) < 0);
     $array0 = CArray::fromElements("d", "e", "f");
     $array1 = CArray::fromElements("a", "e", "f");
     $this->assertTrue(CArray::compare($array0, $array1) > 0);
     $array0 = CArray::fromElements("a", "b");
     $array1 = CArray::fromElements("a", "b", "c");
     $this->assertTrue(CArray::compare($array0, $array1) < 0);
     $array0 = CArray::fromElements(1, 2, 3);
     $array1 = CArray::fromElements(1, 2, 3);
     $this->assertTrue(CArray::compare($array0, $array1) == 0);
     $array0 = CArray::fromElements(1, 2, 3);
     $array1 = CArray::fromElements(4, 5, 6);
     $this->assertTrue(CArray::compare($array0, $array1) < 0);
     $array0 = CArray::fromElements(4, 5, 6);
     $array1 = CArray::fromElements(3, 5, 6);
     $this->assertTrue(CArray::compare($array0, $array1) > 0);
     $array0 = CArray::fromElements(1.2, 3.4, 5.6);
     $array1 = CArray::fromElements(1.2, 3.4, 5.6);
     $this->assertTrue(CArray::compare($array0, $array1) == 0);
     $array0 = CArray::fromElements("a", "b", "c");
     $array1 = CArray::fromElements(u("a"), u("b"), u("c"));
     $this->assertTrue(CArray::compare($array0, $array1) == 0);
     // Using a custom comparator.
     $array0 = CArray::fromElements("a", "b", "c");
     $array1 = CArray::fromElements("A", "B", "C");
     $comparator = function ($string0, $string1) {
         return CString::toLowerCase($string0) === CString::toLowerCase($string1) ? 0 : -1;
     };
     $this->assertTrue(CArray::compare($array0, $array1, $comparator) == 0);
     $array0 = CArray::fromElements(1, 3, 5);
     $array1 = CArray::fromElements(2, 4, 6);
     $comparator = function ($value0, $value1) {
         return CMathi::isOdd($value0) && CMathi::isEven($value1) ? 1 : -1;
     };
     $this->assertTrue(CArray::compare($array0, $array1, $comparator) > 0);
 }
Example #2
0
 /**
  * Determines the order in which two arrays should appear in a place where it matters.
  *
  * You can use your own comparator for the comparison of the elements in the arrays, 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  array $toArray The second array for comparison.
  * @param  callable $comparator **OPTIONAL. Default is** `CComparator::ORDER_ASC`. The function or method to be
  * used for the comparison of any two elements. If this parameter is provided, the comparator should take two
  * parameters, with the first parameter being an element from *this* array and the second parameter being an
  * element from the second array, and return `-1` if the element from *this* array would need to go before the
  * element from the second array if the two were being ordered in separate, `1` if the other way around, and `0` if
  * the two elements are equal.
  *
  * @return int A negative value (typically `-1`) if *this* array should go before the second array, a positive
  * value (typically `1`) if the other way around, and `0` if the two arrays are equal.
  *
  * @link   CComparator.html CComparator
  */
 public function compare($toArray, $comparator = CComparator::ORDER_ASC)
 {
     return CArray::compare($this->m_splArray, $toArray, $comparator);
 }