# sort()

{% code title="Example.php" %}

```php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from(["orange", "banana", "apple", "raspberry", "kiwi"]);


$arrayUtils->sort();
// expected output: ["apple", "banana", "kiwi", "orange", "raspberry"]

$arrayUtils->sort(function($a, $b){ return strcmp($a, $b) * -1; })
// expected output: ["raspberry", "orange", "kiwi", "banana", "apple"]
```

{% endcode %}

## Syntax

```php
$arrayUtils->sort(?callable $callback = null) : ArrayUtils;
```

### Parameter

* `$callback` <img src="/files/-MKk9YjCz_YkVR1YiRy-" alt="" data-size="line">&#x20;

  > A function to compare element for sort, taking two arguments:
  >
  > * `$a` The comparison target A
  > * `$b` The comparison target B
  >
  > Default is `NULL`, If is null, Sort by default sort function.

### Return value

* A sorted array.

## Prefixing

```php
$arrayUtils->sortAs(?callable $callback = null) : array;
```

```php
ArrayUtils::sortFrom(iterable $from, ?callable $callback = null) : ArrayUtils;
```

```php
ArrayUtils::sortFromAs(iterable $from, ?callable $callback = null) : array;
```

## References

{% embed url="<https://www.php.net/manual/en/function.sort.php>" %}

{% embed url="<https://www.php.net/manual/en/function.usort.php>" %}

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://arrayutils.docs.present.kim/methods/c/sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
