Sort by keys

@link http://www.php.net/manual/en/function.ksort.php

@param int $flags
@return bool
496050
public function ksort($flags = SORT_REGULAR)
    {
        $this->_modified = true;
        return ksort($this->_storage, $flags);
    }
1247993
public static function recKsort(&$arr, $flag = SORT_REGULAR)
    {
        if (!is_array($arr)) {
            return false;
        }
        ksort($arr, $flag);
        foreach ($arr as &$arr1) {
            self::recKsort($arr1, $flag);
        }
        return true;
    }
232827
public function setFlags( $flags ) {
		$add = [];
		$remove = [];
		$classPrefix = 'oo-ui-flaggedElement-';

		if ( is_string( $flags ) ) {
			// Set
			if ( !isset( $this->flags[$flags] ) ) {
				$this->flags[$flags] = true;
				$add[] = $classPrefix . $flags;
			}
		} elseif ( is_array( $flags ) ) {
			foreach ( $flags as $key => $value ) {
				if ( is_numeric( $key ) ) {
					// Set
					if ( !isset( $this->flags[$value] ) ) {
						$this->flags[$value] = true;
						$add[] = $classPrefix . $value;
					}
				} else {
					if ( $value ) {
						// Set
						if ( !isset( $this->flags[$key] ) ) {
							$this->flags[$key] = true;
							$add[] = $classPrefix . $key;
						}
					} else {
						// Remove
						if ( isset( $this->flags[$key] ) ) {
							unset( $this->flags[$key] );
							$remove[] = $classPrefix . $key;
						}
					}
				}
			}
		}

		$this->flagged
			->addClasses( $add )
			->removeClasses( $remove );

		return $this;
	}
1137459
public function sort($key, $asc = true)
	{
		return $this->sortByCallback(function($a, $b) use ($key, $asc)
		{
			if (!isset($a->$key) || !isset($b->$key))
			{
				if (!isset($a->$key))
				{
					return ($asc) ? -1 :  1;
				}

				if (!isset($b->$key))
				{
					return ($asc) ?  1 : -1;
				}

				return 0;
			}

			if ($asc)
			{
				return strnatcmp($a->$key, $b->$key);
			}
			else
			{
				return strnatcmp($b->$key, $a->$key);
			}
		});
	}
370576
public function rsort(int $sort_flags = 0): self
    {
        $this->generatorToArray();

        \rsort($this->array, $sort_flags);

        return $this;
    }
980705
private function parseFlag($flags, $flag) {
        $flag = strtolower($flag);

        if (property_exists($flags[0], strtoupper($flag))) {
            $this->flags->put($flag, $flags[0]->{strtoupper($flag)});
        } elseif (property_exists($flags[0], ucfirst($flag))) {
            $this->flags->put($flag, $flags[0]->{ucfirst($flag)});
        } elseif (property_exists($flags[0], $flag)) {
            $this->flags->put($flag, $flags[0]->$flag);
        }
    }
494523
public function add(...$flags)
    {
        foreach ($flags as $flag) {
            if ($this->has($flag)) {
                continue;
            }
            foreach ($this->getValuesFromFlagOrEnum($flag) as $value) {
                $this->value = $this->value | (int) $value;
            }
        }

        return $this;
    }
818869
public function getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING): CachingIterator
    {
        return new CachingIterator($this->getIterator(), $flags);
    }
532719
public function asort($flags = SORT_REGULAR)
    {
        $this->_modified = true;
        return asort($this->_storage, $flags);
    }
395588
public function hasFlag($flag) : bool
    {
        if (is_array($flag)) {
            return $this->hasFlags($flag);
        }

        return in_array($flag, $this->getUserFlags());
    }
947165
public static function sortHeaders($messages, $field, $flag)
    {
        // Strategy: First, we'll create an "index" array.
        // Then, we'll use sort() on that array, and use that to sort the main array.

        $field  = empty($field) ? 'uid' : strtolower($field);
        $flag   = empty($flag) ? 'ASC' : strtoupper($flag);
        $index  = array();
        $result = array();

        reset($messages);

        foreach ($messages as $key => $headers) {
            $value = null;

            switch ($field) {
            case 'arrival':
                $field = 'internaldate';
            case 'date':
            case 'internaldate':
            case 'timestamp':
                $value = rcube_utils::strtotime($headers->$field);
                if (!$value && $field != 'timestamp') {
                    $value = $headers->timestamp;
                }

                break;

            default:
                // @TODO: decode header value, convert to UTF-8
                $value = $headers->$field;
                if (is_string($value)) {
                    $value = str_replace('"', '', $value);
                    if ($field == 'subject') {
                        $value = preg_replace('/^(Re:\s*|Fwd:\s*|Fw:\s*)+/i', '', $value);
                    }

                    $data = strtoupper($value);
                }
            }

            $index[$key] = $value;
        }

        if (!empty($index)) {
            // sort index
            if ($flag == 'ASC') {
                asort($index);
            }
            else {
                arsort($index);
            }

            // form new array based on index
            foreach ($index as $key => $val) {
                $result[$key] = $messages[$key];
            }
        }

        return $result;
    }
1288945
public function sort(array $sorts = array())
    {
        if (func_num_args() === 0) {
            return $this->sorts;
        }

        $this->sorts = array();

        foreach ($sorts as $key => $value) {
            if ($key[0] === '$') {
                $key[0] = '_';
            }

            $this->sorts[$key] = $value;
        }

        return $this;
    }
963997
public function setFlags($flags)
    {
        if ($flags !== null) {
            if (! in_array($flags, [State::SAVED, State::DELETED, State::DELETED | State::SAVED], true)) {
                $message = 'Flags were %s, but need to be State::SAVED, State::DELETED or State::DELETED | State::SAVED';
                throw new InvalidArgumentException(sprintf($message, $flags));
            }

            $this->flags = $flags;
        }

        return $this;
    }
918765
public function hasFlag($flags)
    {
        $mask = $this->getMaskFromFlags($flags);

        return ($this->bitmask & $mask) === $mask;
    }
936659
public static function sortMultisigKeys(array $pubKeys) {
        $result = array_values($pubKeys);
        usort($result, function (PublicKeyInterface $a, PublicKeyInterface $b) {
            $av = $a->getHex();
            $bv = $b->getHex();
            return $av == $bv ? 0 : $av > $bv ? 1 : -1;
        });

        return $result;
    }
301491
public function setSortable($sortable, $sortable_flags = null)
    {
        $this->sortable = (bool) $sortable;
        $this->sortable_flags = $sortable_flags;

        return $this;
    }
993002
public static function sortBy($collection = null, $iterator = null, $sort_flag = null)
    {
        $results = array();
        foreach ($collection as $k => $item) {
            if (is_callable($iterator)) {
                $results[$k] = $iterator($item);
            }
            if (is_object($item)) {
                $results[$k] = $item->$iterator;
            } elseif (is_array($item)) {
                $results[$k] = $item[$iterator];
            }
        }
        $sort_flag = (is_null($sort_flag)) ? SORT_REGULAR : $sort_flag;
        asort($results, $sort_flag);
        foreach ($results as $k => $v) {
            $results[$k] = $collection[$k];
        }
        return $results;
    }