Sort by applying a CallbackHeap and building a new heap
Can be efficient for sorting large stored objects.

@param callable $cb The comparison callback
@return static
16969
protected function heapSort(callable $cb)
    {
        $h = new CallbackHeap($cb);
        foreach ($this as $el) {
            $h->insert($el);
        }

        return static::fromItems($h);
    }
1046896
public function sort(callable $callback = null) : ArrayList
    {
        $values = $this->_sort($callback);
        $this->setValues($values);
        return $this;
    }
337603
public function sort(Closure $callback = null): CollectionInterface
    {
        $array = $this->toArray();

        if (is_null($callback)) {
            sort($array);
        } else {
            usort(
                $array,
                $callback
            );
        }

        return static::make($array);
    }
1257700
public static function call($cb, array $args = [])
    {
        $ret = null;
        if (\is_object($cb) || (\is_string($cb) && \function_exists($cb))) {
            $ret = $cb(...$args);
        } elseif (\is_array($cb)) {
            list($obj, $mhd) = $cb;
            $ret = \is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
        } else {
            if (SWOOLE_VERSION >= '4.0') {
                $ret = call_user_func_array($cb, $args);
            } else {
                $ret = \Swoole\Coroutine::call_user_func_array($cb, $args);
            }
        }

        return $ret;
    }
1202884
public function sort($callback = null)
    {
        if (is_callable($callback)) {
            usort($this->attributes, $callback);
        } else {
            sort($this->attributes);
        }

        return $this;
    }
304223
function sortCustomKey(callable $closure) {
        $data = $this->data;
        \uksort($data, $closure);
        
        return (new self($data));
    }
761210
public function sorted(?callable $comparator = null): MapInterface
    {

        $items = $comparator ?
            $this->items->sorted($comparator) :
            $this->items->sorted();

        return $this->duplicate($items);
    }
339498
public function sortBy(\Closure $callback , $asc = false)
    {
        $results = array();

        foreach ($this->_items as $key => $value) {
            $results[$key] = $callback($value);
        }

        if ($asc) {
            asort($results);
        } else {
            arsort($results);
        }

        foreach (array_keys($results) as $key) {
            $results[$key] = $this->_items[$key];
        }

        $this->_items = $results;

        return $this;
    }
98525
public function sort(callable $callback = null)
    {
        $items = $this->items;

        $callback = $callback ?: function ($a, $b) {
            return $a == $b ? 0 : (($a < $b) ? -1 : 1);

        };

        uasort($items, $callback);

        return new static($items);
    }
963703
public function partition($callback)
    {
        $partitions = [new static, new static];
        $callback   = $this->valueAccessor($callback);

        foreach ($this->items as $key => $item) {
            $partitions[(int) ! $callback($item)][$key] = $item;
        }

        return new static($partitions);
    }
620996
public function bind($event, $cb)
    {
        if ($cb !== null) {
            $cb = CallbackWrapper::wrap($cb);
        }
        $event = (array) $event;
        foreach ($event as $e) {
            CallbackWrapper::addToArray($this->eventHandlers[$e], $cb);
        }
        return $this;
    }
791326
public function sort(callable $comparator = null): ArrayCollection
    {
        $items = $this->items;

        $comparator ? uasort($items, $comparator) : uasort($items, function ($a, $b) {
            return $a <=> $b;
        });

        return new static($items);
    }
1096982
public function filter(callable $cb)
    {
        $count = count($this->sfa);
        $sfa = new SplFixedArray($count);
        $newCount = 0;

        foreach ($this->sfa as $el) {
            if ($cb($el)) {
                $sfa[$newCount++] = $el;
            }
        }

        $sfa->setSize($newCount);
        return new static($sfa);
    }
1033167
public function filter($cb)
    {
        $r = [];

        foreach ($this->storage as $key => $value) {
            if (call_user_func_array($cb, [$value, $key])) {
                $r[] = $this->storage[$key];
            }
        }

        return new Collection($r);
    }
364126
public function each( callable $callback, $count = 1000 )
	{
		if ( is_null( $this->query->orders ) && is_null( $this->query->unionOrders ) )
		{
			$this->orderBy( $this->model->getQualifiedKeyName(), 'asc' );
		}

		return $this->chunk( $count, function ( $results ) use ( $callback )
		{
			foreach ( $results as $key => $value )
			{
				if ( $callback( $value, $key ) === false )
				{
					return false;
				}
			}
		} );
	}
1076299
protected function quickSort(callable $cb)
    {
        $sfa = new SplFixedArray(count($this));

        // Create an auxiliary stack
        $stack = new SplStack();

        // initialize top of stack
        // push initial values of l and h to stack
        $stack->push([0, count($sfa) - 1]);

        $first = true;
        // Keep popping from stack while is not empty
        while (!$stack->isEmpty()) {
            // Pop h and l
            list($lo, $hi) = $stack->pop();

            if ($first) {
                // Start our partition iterator on the original data
                $partition = new LimitIterator($this->sfa, $lo, $hi - $lo);
            } else {
                $partition = new LimitIterator($sfa, $lo, $hi - $lo);
            }
            $ii = $partition->getInnerIterator();

            // Set pivot element at its correct position in sorted array
            $x = $ii[$hi];
            $i = ($lo - 1);

            foreach ($partition as $j => $el) {
                if ($cb($ii[$j], $x) <= 0) {
                    // Bump up the index of the last low hit, and swap
                    $i++;
                    $temp = $sfa[$i];
                    $sfa[$i] = $el;
                    $sfa[$j] = $temp;
                } elseif ($first) {
                    $sfa[$j] = $el;
                }
            }
            $sfa[$hi] = $x;

            // Set the pivot element
            $pivot = $i + 1;
            // Swap the last hi with the second-last hi
            $sfa[$hi] = $sfa[$pivot];
            $sfa[$pivot] = $x;

            // If there are elements on left side of pivot, then push left
            // side to stack
            if ($pivot - 1 > $lo) {
                $stack->push([$lo, $pivot - 1]);
            }

            // If there are elements on right side of pivot, then push right
            // side to stack
            if ($pivot + 1 < $hi) {
                $stack->push([$pivot + 1, $hi]);
            }
        }

        return new static($sfa);
    }
520184
public function query($q, $cb = null)
    {
        if ($this->finished) {
            throw new ConnectionFinished;
        }
        return $this->command(Pool::COM_QUERY, $q, $cb);
    }