Documentation

Sort
in package

Utility function for sorting

Tags
author

Norbert Wagner

Table of Contents

Methods

buildArrayCompareBy()  : callable
Returns a closure that compares two array values

Methods

buildArrayCompareBy()

Returns a closure that compares two array values

public static buildArrayCompareBy(string $field) : callable

Uses php's strcmp function for comparison.

Useful to compare multi-dimensional arrays with sorting functions like usort

Example:

$array = [
  0 => ['id' => 0, 'name' => 'my name'],
  1 => ['id' => 1, 'name' => 'a name']
];

usort($array, VeruA\Util\Sort::buildArrayCompareBy('name'));

// results in:
[
  0 => ['id' => 1, 'name' => 'a name'],
  1 => ['id' => 0, 'name' => 'my name']
];
Parameters
$field : string

the index of the Array to use for comparison

Return values
callable

        
On this page

Search results