arifulhb/php-algorithms

Multi-dimensional Array & Object Sorting in PHP

v0.1.1 2021-06-08 15:40 UTC

This package is not auto-updated.

Last update: 2024-04-25 04:03:58 UTC


README

Sort a multidimensional array or object by String or integer or both types.

Installation

Install the package on your php application.

composer require arifulhb/php-algorithms

Sort by Array

For example, if you have an array, and you want to sort by name or by price or both, you can use this package to sort.

 $items = [
    [
        'name' => 'computer',
        'price' => 4,
    ],
    [
        'name' => 'tv',
        'price' => 9,
    ],
    [
        'name' => 'apple',
        'price' => 1,
    ],
    [
        'name' => 'airpod',
        'price' => 1,
    ],
    [
        'name' => 'airtag',
        'price' => 3,
    ],
    [
        'name' => 'shampoo',
        'price' => 5,
    ]
];

How to

// create a configuration object specifying integer and string field accordingly.
$config = new SortConfiguration('price', 'name');

// create sort object with the configuration
$sortArray = new SortArray($config);

// sort the array
$result = $sortArray->sort($products);

Sort by Object

Same as sort by array, but here you may have an array of object to sort. For example,

$arrayOfObjects = [
    new Product('computer', 4),
    new Product('tv', 9),
    new Product('apple', 1),
    new Product('airpod', 1),
    new Product('airtag', 3),
    new Product('shampoo', 5)
];

How to

// create a configuration object specifying integer and string field accordingly.
$config = new SortConfiguration('price', 'name');

// create sort object with the configuration
$sortObject = new SortObject($config);

// sort the array
$result = $sortObject->sort($products);

Development

To run the test cases,

composer run-script tests