Tuesday, November 19, 2024
Google search engine
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

Recent Comments