SkillAgentSearch skills...

Sort

A collection of array sorting transforms and functions

Install / Use

/learn @graze/Sort
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Sort

<img src="http://media2.giphy.com/media/fAaAo6SyjVJf2/200.gif" alt="sort" align="right" height=200/>

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]));
<!-- Links -->

Related Skills

View on GitHub
GitHub Stars12
CategoryDevelopment
Updated6y ago
Forks1

Languages

PHP

Security Score

75/100

Audited on Sep 19, 2019

No findings