Current Location: Home> Function Categories> sort

sort

Sort arrays
Name:sort
Category:Array
Programming Language:php
One-line Description:Sort the array.

Definition and usage

sort() function sorts the index array ascending order.

Note: This function assigns new key names to cells in an array. The original key name will be deleted.

Return TRUE if successful, otherwise return FALSE.

Tip: Please use the rsort() function to sort the index array in descending order.

Example

Example 1

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

 <?php
$cars = array ( "Volvo" , "BMW" , "Toyota" ) ;
sort ( $cars ) ;
?>

Try it yourself

Example 2

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

 <?php
$numbers = array ( 4 , 6 , 2 , 22 , 11 ) ;
sort ( $numbers ) ;
?>

Try it yourself

grammar

 sort ( array , sortingtype ) ;
parameter describe
array Required. Specifies the array to be sorted.
sortingtype

Optional. Specifies how to compare elements/items of an array. Possible values:

  • 0 = SORT_REGULAR - Default. Arrange each item in a regular order (Standard ASCII, without changing the type)
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Handle each item as a string.
  • 3 = SORT_LOCALE_STRING - Handle each item as a string, based on the current locale setting (can be changed via setlocale()).
  • 4 = SORT_NATURAL - Handle each item as a string, using a natural sort like natsort().
  • 5 = SORT_FLAG_CASE - Strings can be sorted in combination with (bit-by-bit or) SORT_STRING or SORT_NATURAL, case-insensitive.
Similar Functions
Popular Articles