PHP sort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, sort() sorts the values of an array from lowest to highest and gives them new keys 0, 1, 2 and so on.

It is the most common sorting function. It works for numbers and for strings. The array is changed in place, and any old keys are thrown away. Use asort() if you need to keep the keys.

Syntax

Call sort() with the values you need. Start with this line.

PHP
sort($numbers);
CallWhat it does
sort($numbers)Sorts the values from lowest to highest.
sort($words, SORT_STRING)Compares the values as text.
rsort($numbers)The descending version of the same sort.

sort() changes the original array and returns true. All old keys are replaced by the new keys 0, 1, 2 and so on. To sort without losing keys, use asort() or ksort().

Library declarations
PHP
sort(array &$array, int $flags = SORT_REGULAR): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.
flagsOptional. How to compare the values. SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE combined with SORT_STRING or SORT_NATURAL.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort Numbers from Low to High

The values are rearranged in ascending order.

PHP
<?php
$numbers = [5, 2, 8, 1];

sort($numbers);

echo implode(", ", $numbers), PHP_EOL;

How It Works

  1. $numbers holds four numbers in no particular order.
  2. sort() puts them in ascending order inside the same variable.
  3. There is no need to save a return value, because the function returns true.
  4. implode() prints the sorted numbers on one line.

Example 2 — Keys Are Replaced

sort() discards the old keys. print_r() shows the new numeric keys.

PHP
<?php
$points = ["x" => 3, "y" => 1, "z" => 2];

sort($points);

print_r($points);

How It Works

  1. The original array has the string keys x, y and z.
  2. sort() orders the values from smallest to largest.
  3. The old keys are removed during sorting.
  4. The new keys 0, 1 and 2 are given in order, as print_r() shows.

Example 3 — Numeric Order Versus Text Order

Numeric strings are compared as numbers by default. SORT_STRING compares them as text.

PHP
<?php
$a = ["10", "9", "2", "1"];
sort($a);
echo implode(", ", $a), PHP_EOL;

sort($a, SORT_STRING);
echo implode(", ", $a), PHP_EOL;

How It Works

  1. By default PHP sees that the strings are numbers and compares them as numbers.
  2. The first line shows 1, 2, 9, 10.
  3. SORT_STRING compares character by character, so "10" comes before "2" because "1" is smaller than "2".
  4. The second line shows 1, 10, 2, 9.

Frequently Asked Questions

It sorts the values of an array in ascending order and renumbers the keys from 0.
No. Use asort() to sort by value and keep keys, or ksort() to sort by key.
Use rsort(). It does the same as sort() but from highest to lowest.
No. It changes the array you pass in and returns true.
Use sort($array, SORT_FLAG_CASE | SORT_STRING). Without it, capital letters sort before lowercase letters.

Next: uasort()

uasort() sorts an array by its values using your own comparison function, and keeps the ke.

uasort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful