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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 );
?>

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.