public function testSort() { $array = CArray::fromElements("oua", "vnf", "fnf", "aod", "tvi", "nbt", "jny", "vor", "rfd", "cvm", "hyh", "kng", "ggo", "uea", "hkb", "qbk", "xla", "uod", "jzi", "chw", "ssy", "olr", "bzl", "oux", "ltk", "bah", "khu", "msr", "pqv", "npb", "mtb", "eku", "vcv", "vbv", "wuo", "lrw", "bkw", "ezz", "jtc", "dwk", "dsq", "kzu", "oey", "vbi", "seh", "klz", "asj", "gzg", "ccs", "qop"); CArray::sort($array, CComparator::ORDER_ASC); $this->assertTrue(CArray::equals($array, CArray::fromElements("aod", "asj", "bah", "bkw", "bzl", "ccs", "chw", "cvm", "dsq", "dwk", "eku", "ezz", "fnf", "ggo", "gzg", "hkb", "hyh", "jny", "jtc", "jzi", "khu", "klz", "kng", "kzu", "lrw", "ltk", "msr", "mtb", "nbt", "npb", "oey", "olr", "oua", "oux", "pqv", "qbk", "qop", "rfd", "seh", "ssy", "tvi", "uea", "uod", "vbi", "vbv", "vcv", "vnf", "vor", "wuo", "xla"))); CArray::sort($array, CComparator::ORDER_DESC); $this->assertTrue(CArray::equals($array, CArray::fromElements("xla", "wuo", "vor", "vnf", "vcv", "vbv", "vbi", "uod", "uea", "tvi", "ssy", "seh", "rfd", "qop", "qbk", "pqv", "oux", "oua", "olr", "oey", "npb", "nbt", "mtb", "msr", "ltk", "lrw", "kzu", "kng", "klz", "khu", "jzi", "jtc", "jny", "hyh", "hkb", "gzg", "ggo", "fnf", "ezz", "eku", "dwk", "dsq", "cvm", "chw", "ccs", "bzl", "bkw", "bah", "asj", "aod"))); $array = CArray::fromElements(5, 2, 1, 3, 4); CArray::sort($array, CComparator::ORDER_ASC); $this->assertTrue(CArray::equals($array, CArray::fromElements(1, 2, 3, 4, 5))); // Special cases. $array = CArray::fromElements("a"); CArray::sort($array, CComparator::ORDER_ASC); $this->assertTrue(CArray::equals($array, CArray::fromElements("a"))); $array = CArray::make(); CArray::sort($array, CComparator::ORDER_ASC); $this->assertTrue(CArray::equals($array, CArray::make())); }
/** * Sorts the elements in an array. * * You can use your own comparator for element comparison, but the default comparators, such as * `CComparator::ORDER_ASC` and `CComparator::ORDER_DESC`, have got you covered when sorting scalar values, such as * `string`, `int`, `float`, and `bool` in the ascending or descending order respectively. And the default * comparators are 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 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, which are the two elements being compared, and return `-1` if the first element needs to go before * the second element in the sorted array, `1` if the other way around, and `0` if the two elements are equal. * * @return void * * @link CComparator.html CComparator */ public function sort($comparator = CComparator::ORDER_ASC) { CArray::sort($this->m_splArray, $comparator); }