Monday, October 6, 2025
HomeLanguagesPHP | collator_sort_with_sort_keys() Function

PHP | collator_sort_with_sort_keys() Function

The collator_sort_with_sort_keys() function is an inbuilt function in PHP which is used to sort array using specified collator and sort keys.

Syntax:

  • Procedural style:
    bool collator_sort_with_sort_keys( $coll, $arr )
  • Object oriented style:
    bool Collator::sortWithSortKeys( $arr )

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

  • $coll: This parameter is used as collator object. It provides the comparison capability with support for appropriate locale-sensitive sort orderings.
  • $arr: This parameter is used to hold the string which needs to sort.

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

Below programs illustrates the collator_sort_with_sort_keys() function in PHP.

Program 1:




<?php
  
// Declare an array which need to sort
$arr  = array( 'Geeks', 'g4g', 'neveropen', 'geek' );
$coll = collator_create( 'gs' );
  
// Sort the array with key value
collator_sort_with_sort_keys( $coll, $arr );
  
var_export( $arr );
?>


Output:

array (
  0 => 'g4g',
  1 => 'geek',
  2 => 'Geeks',
  3 => 'neveropen',
)

Program 2:




<?php
  
// Declare an array which need to sort
$arr  = array( 'Geeks123', 'GeeksABC', 'neveropen', 'Geeks' );
  
// Create collector
$coll = collator_create( 'en_US' );
  
// Sort the array with key value
collator_sort_with_sort_keys( $coll, $arr );
  
var_export( $arr );
?>


Output:

array (
  0 => 'Geeks',
  1 => 'Geeks123',
  2 => 'GeeksABC',
  3 => 'neveropen',
)

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

RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6780 POSTS0 COMMENTS