The sort()
Function in PHP
The sort()
function in PHP is used to sort arrays in ascending order. It rearranges the elements of an array from lowest to highest values.
Syntax
sort(array &$array, int $flags = SORT_REGULAR): bool
Parameters
$array
(required): The input array to be sorted (passed by reference)$flags
(optional): Specifies how to compare items. Possible values:SORT_REGULAR
- Compare items normally (don't change types)SORT_NUMERIC
- Compare items numericallySORT_STRING
- Compare items as stringsSORT_LOCALE_STRING
- Compare items as strings based on current localeSORT_NATURAL
- Compare items as strings using natural orderingSORT_FLAG_CASE
- Can be combined withSORT_STRING
orSORT_NATURAL
for case-insensitive sorting
Return Value
- Returns
true
on success - Returns
false
on failure
Examples
1. Basic Sorting of Numeric Arrays
<?php
$numbers = [4, 2, 8, 6, 1];
sort($numbers);
print_r($numbers);
// Output: Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 6 [4] => 8 )
?>
2. Sorting String Arrays
<?php
$fruits = ["banana", "apple", "cherry", "date"];
sort($fruits);
print_r($fruits);
// Output: Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
?>
3. Sorting with Different Flags
<?php
$mixed = ["10", "2", "100", "25"];
// Regular sorting (as strings)
sort($mixed, SORT_REGULAR);
print_r($mixed);
// Output: Array ( [0] => 10 [1] => 100 [2] => 2 [3] => 25 )
// Numeric sorting
sort($mixed, SORT_NUMERIC);
print_r($mixed);
// Output: Array ( [0] => 2 [1] => 10 [2] => 25 [3] => 100 )
?>
4. Case-Insensitive Sorting
<?php
$words = ["Apple", "banana", "Cherry", "date"];
// Case-sensitive (default)
sort($words);
print_r($words);
// Output: Array ( [0] => Apple [1] => Cherry [2] => banana [3] => date )
// Case-insensitive
sort($words, SORT_STRING | SORT_FLAG_CASE);
print_r($words);
// Output: Array ( [0] => Apple [1] => banana [2] => Cherry [3] => date )
?>
5. Natural Order Sorting
<?php
$files = ["file1.txt", "file10.txt", "file2.txt", "file20.txt"];
// Regular string sorting
sort($files);
print_r($files);
// Output: Array ( [0] => file1.txt [1] => file10.txt [2] => file2.txt [3] => file20.txt )
// Natural order sorting
sort($files, SORT_NATURAL);
print_r($files);
// Output: Array ( [0] => file1.txt [1] => file2.txt [2] => file10.txt [3] => file20.txt )
?>
6. Important Note: Index Reassignment
<?php
$associative = [
"b" => "banana",
"a" => "apple",
"c" => "cherry"
];
echo "Before sorting (associative array):\n";
print_r($associative);
// Output: Array ( [b] => banana [a] => apple [c] => cherry )
sort($associative);
echo "After sort() - keys are lost and reassigned:\n";
print_r($associative);
// Output: Array ( [0] => apple [1] => banana [2] => cherry )
?>
Important Characteristics
1. Modifies Original Array
<?php
$original = [3, 1, 2];
sort($original);
// $original is now [1, 2, 3]
?>
2. Key Reassignment
<?php
$array = [10 => "ten", 5 => "five", 20 => "twenty"];
sort($array);
// Keys are reset to 0, 1, 2: [0 => "five", 1 => "ten", 2 => "twenty"]
?>
3. Return Value Usage
<?php
$array = [3, 1, 2];
if (sort($array)) {
echo "Array sorted successfully!";
} else {
echo "Sorting failed!";
}
?>
Related Sorting Functions
Function | Description | Preserves Keys? |
---|---|---|
sort() |
Sorts array in ascending order | No |
rsort() |
Sorts array in descending order | No |
asort() |
Sorts associative array by values | Yes |
ksort() |
Sorts associative array by keys | Yes |
usort() |
Sorts array using user-defined function | No |
Practical Example: Sorting User Data
<?php
$users = [
["name" => "John", "age" => 25],
["name" => "Alice", "age" => 30],
["name" => "Bob", "age" => 22]
];
// Extract ages for sorting
$ages = array_column($users, 'age');
sort($ages);
echo "Ages in ascending order:\n";
print_r($ages);
// Output: Array ( [0] => 22 [1] => 25 [2] => 30 )
?>
Best Practices
- Use appropriate flags for the data type you're sorting
- Be aware that keys are reassigned - use
asort()
if you need to preserve keys - For associative arrays, consider using
asort()
orksort()
instead - For complex sorting, use
usort()
with a custom comparison function
The sort()
function is efficient and perfect for simple sorting tasks where you don't need to preserve the original array keys.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.