Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP sort() Function

PHP sort() Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Sort the elements of the $cars array in ascending alphabetical order:

Run Example »


Definition and Usage

The sort() function sorts a numeric array in ascending order.

**Tip:** Please use rsort() function sorts a numeric array in descending order.

Syntax

sort(

array,sortingtype

);

Parameters Description
array Required. Specifies the array to sort.
sortingtype Optional. Specifies how to arrange the elements/items of the array. Possible values:
  • 0 = SORT_REGULAR - Default. Compare items normally (Standard ASCII, do not change types).
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Treat each item as a string.
  • 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed by setlocale() ).
  • 4 = SORT_NATURAL - Treat each item as a string using natural ordering similar to natsort() natural ordering.
  • 5 = SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.
  • |

    Technical Details

    Return Value: Returns TRUE on success, FALSE on failure.
    PHP Version: 4+

    More examples

    Example 1

    Sort the elements of the $numbers array in ascending numeric order:

    Run Example »


    PHP Array ReferenceComplete PHP Array Reference Manual