graze/sort

A collection of array sorting transforms and functions

2.0.1 2014-09-23 17:01 UTC

This package is auto-updated.

Last update: 2024-02-24 03:53:09 UTC


README

sort

Master build: Master branch build status

This library provides a few useful functions used for sorting large arrays or sorting where comparing array values is expensive to perform. The intention is to provide these functions while remaining interoperable with PHP's built-in usort functions where possible.

It can be installed in whichever way you prefer, but we recommend Composer.

{
    "require": {
        "graze/sort": "*"
    }
}

Documentation

<?php

$unsorted = [
    (object) ['foo' => 1, 'bar' => 3],
    (object) ['foo' => 3, 'bar' => 2],
    (object) ['foo' => 2, 'bar' => 1],
    (object) ['foo' => 2, 'bar' => 2],
    (object) ['foo' => 3, 'bar' => 3],
    (object) ['foo' => 1, 'bar' => 1],
    (object) ['foo' => 2, 'bar' => 3],
    (object) ['foo' => 3, 'bar' => 1],
    (object) ['foo' => 1, 'bar' => 2]
];

$foo = function ($v) { return $v->foo; };
$bar = function ($v) { return $v->bar; };

// Using comparison sorting
$sorted = \Graze\Sort\comparison($unsorted, [$foo, $bar]);

// Using schwartzian sorting
$sorted = \Graze\Sort\schwartzian($unsorted, [$foo, $bar]);

// Using comparison sorting with usort
$sorted = $unsorted;
usort($sorted, \Graze\Sort\comparison_fn($unsorted, [$foo, $bar]));