Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
core_collator Class Reference

A collator class with static methods that can be used for sorting. More...

Static Public Member Functions

static asort (array &$arr, $sortflag=core_collator::SORT_STRING)
 Locale aware sorting, the key associations are kept, values are sorted alphabetically.
 
static asort_array_of_arrays_by_key (array &$array, $key, $sortflag=core_collator::SORT_STRING)
 Locale aware sort of array of arrays.
 
static asort_objects_by_method (array &$objects, $method, $sortflag=core_collator::SORT_STRING)
 Locale aware sort of objects by a method in common to all objects.
 
static asort_objects_by_property (array &$objects, $property, $sortflag=core_collator::SORT_STRING)
 Locale aware sort of objects by a property in common to all objects.
 
static callback_naturalise ($matches)
 
static ksort (array &$arr, $sortflag=core_collator::SORT_STRING)
 Locale aware sorting, the key associations are kept, keys are sorted alphabetically.
 

Public Attributes

const CASE_SENSITIVE = 64
 @const do not ignore case when sorting, use bitwise "|" with SORT_NATURAL or SORT_STRING, equivalent to Collator\UPPER_FIRST
 
const SORT_NATURAL = 6
 @const compare items like natsort(), equivalent to SORT_NATURAL
 
const SORT_NUMERIC = 2
 @const compare items as numbers, equivalent to Collator\SORT_NUMERIC
 
const SORT_REGULAR = 0
 @const compare items using general PHP comparison, equivalent to Collator\SORT_REGULAR, this may bot be locale aware!
 
const SORT_STRING = 1
 @const compare items as strings, equivalent to Collator\SORT_STRING
 

Static Protected Member Functions

static ensure_collator_available ()
 Ensures that a collator is available and created.
 
static naturalise ($string)
 Normalise numbers in strings for natural sorting comparisons.
 
static restore_array (array &$arr, array &$original)
 Restore array contents keeping new keys.
 

Static Protected Attributes

static Collator false null $collator = null
 
static string null $locale = null
 The locale that was used in instantiating the current collator.
 

Detailed Description

A collator class with static methods that can be used for sorting.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Member Function Documentation

◆ asort()

static core_collator::asort ( array & $arr,
$sortflag = core_collator::SORT_STRING )
static

Locale aware sorting, the key associations are kept, values are sorted alphabetically.

Parameters
array$arrarray to be sorted (reference)
int$sortflagOne of core_collator\SORT_NUMERIC, core_collator\SORT_STRING, core_collator\SORT_NATURAL, core_collator\SORT_REGULAR optionally "|" core_collator\CASE_SENSITIVE
Return values
boolTrue on success

◆ asort_array_of_arrays_by_key()

static core_collator::asort_array_of_arrays_by_key ( array & $array,
$key,
$sortflag = core_collator::SORT_STRING )
static

Locale aware sort of array of arrays.

Given an array like: $array = array( array('name' => 'bravo'), array('name' => 'charlie'), array('name' => 'alpha') );

If you call: core_collator\asort_array_of_arrays_by_key($array, 'name')

You will be returned $array sorted by the name key of the subarrays. e.g. $array = array( array('name' => 'alpha'), array('name' => 'bravo'), array('name' => 'charlie') );

Parameters
array$arrayAn array of objects to sort (handled by reference)
string$keyThe key to use for comparison
int$sortflagOne of core_collator\SORT_NUMERIC, core_collator\SORT_STRING, core_collator\SORT_NATURAL, core_collator\SORT_REGULAR optionally "|" core_collator\CASE_SENSITIVE
Return values
boolTrue on success

◆ asort_objects_by_method()

static core_collator::asort_objects_by_method ( array & $objects,
$method,
$sortflag = core_collator::SORT_STRING )
static

Locale aware sort of objects by a method in common to all objects.

Parameters
array$objectsAn array of objects to sort (handled by reference)
string$methodThe method to call to generate a value for comparison
int$sortflagOne of core_collator\SORT_NUMERIC, core_collator\SORT_STRING, core_collator\SORT_NATURAL, core_collator\SORT_REGULAR optionally "|" core_collator\CASE_SENSITIVE
Return values
boolTrue on success

◆ asort_objects_by_property()

static core_collator::asort_objects_by_property ( array & $objects,
$property,
$sortflag = core_collator::SORT_STRING )
static

Locale aware sort of objects by a property in common to all objects.

Parameters
array$objectsAn array of objects to sort (handled by reference)
string$propertyThe property to use for comparison
int$sortflagOne of core_collator\SORT_NUMERIC, core_collator\SORT_STRING, core_collator\SORT_NATURAL, core_collator\SORT_REGULAR optionally "|" core_collator\CASE_SENSITIVE
Return values
boolTrue on success

◆ ensure_collator_available()

static core_collator::ensure_collator_available ( )
staticprotected

Ensures that a collator is available and created.

Return values
boolReturns true if collation is available and ready

◆ ksort()

static core_collator::ksort ( array & $arr,
$sortflag = core_collator::SORT_STRING )
static

Locale aware sorting, the key associations are kept, keys are sorted alphabetically.

Parameters
array$arrarray to be sorted (reference)
int$sortflagOne of core_collator\SORT_NUMERIC, core_collator\SORT_STRING, core_collator\SORT_NATURAL, core_collator\SORT_REGULAR optionally "|" core_collator\CASE_SENSITIVE
Return values
boolTrue on success

◆ naturalise()

static core_collator::naturalise ( $string)
staticprotected

Normalise numbers in strings for natural sorting comparisons.

Parameters
string$string
Return values
stringstring with normalised numbers

◆ restore_array()

static core_collator::restore_array ( array & $arr,
array & $original )
staticprotected

Restore array contents keeping new keys.

Parameters
array$arr
array$original
Return values
voidmodifies $arr

The documentation for this class was generated from the following file: