tsoffereins/number-sorter

A PHP library that helps you sort your numbers alphabetically.

v1.0.0 2019-04-06 16:06 UTC

This package is auto-updated.

Last update: 2024-04-07 04:46:08 UTC


README

With this library you can finally sort your list of numbers the way you always wanted: alphabetically.

Installation

You can install the NumberSorter through composer:

composer require tsoffereins/number-sorter

This library requires PHP 5.6 or higher in order to work.

Usage

Sorting a list of number alphabetically is easy as 1 3 2:

$sorter = new \NumberSorter\NumberSorter();

$input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$output = $sorter->sort($input);

var_dump($output); // [8, 5, 4, 9, 1, 7, 6, 10, 3, 2]

Numeric list

The input list must be an array of only numeric values. This includes any value that would make is_numeric() return true. See php.net for more on that.

If you pass a non numeric value a TypeError will be thrown.

Language

By default numbers are sorted alphabetically in English, but if you'd like to sort in another language you can pass it to the constructor:

$sorter = new \NumberSorter\NumberSorter('nl');

$input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$output = $sorter->sort($input);

var_dump($output); // [8, 3, 1, 9, 10, 2, 4, 5, 6, 7]

The NumberSorter library relies on the package kwn/number-to-words for translating numbers into words. Take a look at their documentation for the available locales.

Immutability

Passing an array by reference and sorting its contents, like PHP's sort() function does, might have been OK in the nineties, but proper code should limit side effects. Therefore, the NumberSorter does not alter the input array but returns a new array with the same numbers sorted alphabetically.

Support

Are you kidding me?!