2017 © Pedro PelĆ”ez
 

library spl-collections-sort

Helper library for sorting Spl data structures

image

the-jj/spl-collections-sort

Helper library for sorting Spl data structures

  • Monday, October 16, 2017
  • by the-jj
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

SPL collections sort

Latest Stable Version Latest Unstable Version Build Status Coverage Status SensioLabsInsight License, (*1)

Several methods for sorting SPL datastructures. Currently only SplFixedArray objects are supported. Should be fast with big arrays as well., (*2)

Basic usage

All sorting algorithms accept SplFixedArray object as first argument and optional comparison callback function as second argument. Sorting is done in place., (*3)

Insertion sort

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::insertionSort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]

Custom comparison function

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::insertionSort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]

Boundaries

Additionally, insertion sort accepts two more arguments - boundaries $low and $high:, (*4)

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::insertionSort($splFixedArray, null, 2, 4);

var_dump($splFixedArray->toArray()); // [5, 3, 0, 6, 8, 4]

Quicksort

Non-recursive implementation is used, which makes it viable for sorting big arrays. Upon reaching subsets of 5 elements or less, falls back to insertion sort., (*5)

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::quickSort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]

Custom comparison function

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::quickSort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]

Mergesort

A bottom-up (non-recursive) implementation is used., (*6)

Usage? You guessed it:, (*7)

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::mergeSort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]

Custom comparison function

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::mergeSort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]

Array sort

There is one more sorting method - array sort that uses PHP's sort() (or usort()) method. Usage is same as with the above algorithms:, (*8)

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::arraySort($splFixedArray);

var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]

Custom comparison function

$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]);

SplFixedArraySort::arraySort($splFixedArray, function($a, $b) {
    if ($a < $b) return 1;
    if ($a > $b) return -1;
    return 0;
});

var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]

Why?

When using a regular PHP array, we are offered a vast array (no pun intended) of sorting functions. However, no such thing is offered for SplFixedArray objects in vanilla PHP., (*9)

This library is to change that. Currently it offers several methods for sorting SplFixedArray objects. In the future, I might add support for other structures (that make sense to be sorted, like doubly-linked list?)., (*10)

Installation

composer require the-jj/spl-collections-sort

The Versions

16/10 2017

dev-master

9999999-dev

Helper library for sorting Spl data structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Juraj Juričić
by Juraj Juričić

16/10 2017

1.0.3

1.0.3.0

Helper library for sorting Spl data structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Juraj Juričić

25/08 2017

1.0.2

1.0.2.0

Helper library for sorting Spl data structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Juraj Juričić

25/08 2017

1.0.1

1.0.1.0

Helper library for sorting Spl data structures

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Juraj Juričić

25/08 2017

1.0.0

1.0.0.0

Helper library for sorting Spl data structures

  Sources   Download

MIT

The Development Requires

by Juraj Juričić