Search Results
natsort (PHP 4, PHP 5, PHP 7, PHP 8) natsort — Sort an array using a "natural order" algorithm
Definition and Usage The natsort () function sorts an array by using a "natural order" algorithm. The values keep their original keys. In a natural algorithm, the number 2 is less than the number 10. In computer sorting, 10 is less than 2, because the first number in "10" is less than 2.
- Description
- Syntax
- More Examples
The natsort()function sorts an array using a "natural order" algorithm. The keys are preserved, i.e. the key-to-value mapping will remain unchanged by the sort operation. The following table summarizes the technical details of this function.
The basic syntax of the natsort()function is given with: The following example shows the natsort()function in action.
Here're some more examples showing how natsort()function actually works: The following example shows how negative numeric strings will be sorted in natural ordering. The following example shows how zero padded numeric strings will be sorted in natural ordering.
- Returns TRUE on success or FALSE on failure.
- PHP 4+
Learn how to use the natsort function in PHP for natural order sorting of arrays. Enhance your PHP skills with practical examples.
This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in sort) can be seen in the example below. Note:
Jul 11, 2025 · The natsort () function of the ArrayObject class in PHP is used to sort the elements of the ArrayObject following a natural order sorting algorithm. The natsort () function is used to sort alphanumeric strings in a order a normal human being would do.
People also ask
What does natsort() do in PHP?
How to sort an array using natsort in PHP?
What is natsort() function?
Does natsort() always return the expected sort order?
Mar 13, 2025 · PHP natsort function tutorial shows how to sort arrays in natural order in PHP. Learn natsort with practical examples.
