Saturday, June 13, 2026
HomeLanguagesPHP | collator_sort() Function

PHP | collator_sort() Function

The collator_sort() function is an inbuilt function in PHP which is used to sort an array using specified collator. This function returns True on success or False on failure.

Syntax:

  • Procedural style:
    bool collator_sort( $coll, $arr, $sort_flag )
  • Object oriented style:
    bool Collator::sort( $arr, $sort_flag )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $coll: This parameter is used as collator object.
  • $arr: This parameter contains array of which needs to sort.
  • $sort_flag: It is optional parameter which define sorting type, one of the following:
    • Collator::SORT_REGULAR: It compare items normally. It is the default sorting.
    • Collator::SORT_NUMERIC: It compare the items numerically.
    • Collator::SORT_STRING: It compare the items as strings.

Return Value: This function returns True on success or false on failure.

Below programs illustrate the collator_sort() function in PHP:

Program 1:




<?php
$coll = collator_create( 'en_US' );
  
// Declare array and initialize it
$arr  = array( 'geek', 'geeK', 'Geek', 'neveropen' );
  
// Sort array
collator_sort( $coll, $arr );
  
// Display array content
var_export( $arr );
?>


Output:

array (
  0 => 'geek',
  1 => 'geeK',
  2 => 'Geek',
  3 => 'neveropen',
)

Program 2:




<?php
$coll = collator_create( 'en_US' );
  
// Declare array and initialize it
$arr  = array( 30, 12, 56, 33, 74, 23, 1 );
  
// Sort array
collator_sort( $coll, $arr );
  
// Display array content
var_export( $arr );
?>


Output:

array (
  0 => 1,
  1 => 12,
  2 => 23,
  3 => 30,
  4 => 33,
  5 => 56,
  6 => 74,
)

Related Articles:

Reference: http://php.net/manual/en/collator.sort.php

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS