A collection of array sorting transforms and functions

A collection of array sorting transforms and functions

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 = GrazeSortcomparison($unsorted, [$foo, $bar]);

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

// Using comparison sorting with usort
$sorted = $unsorted;
usort($sorted, GrazeSortcomparison_fn($unsorted, [$foo, $bar]));

Source

Add a Comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *