2017 © Pedro Peláez
 

library sorter

Sort multidimensional arrays and objects.

image

sebastiansulinski/sorter

Sort multidimensional arrays and objects.

  • Thursday, January 28, 2016
  • by sebastiansulinski
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,877 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 11 % Grown

The README.md

Sorter

This package allows you to sort multidimensional arrays and objects., (*1)

Installation

Install package via composer, (*2)

composer require sebastiansulinski/sorter

Usage instructions

1. Simple array

$array = [
    [
        'name' => 'Arthur'
    ],
    [
        'name' => 'Tom'
    ],
    [
        'name' => 'Mark'
    ]
];

$sorter = new Sorter($array);

// sort the array by 'name' key
$sorter->asc('name');

// two ways of printing array
echo $sorter->printR();
echo $sorter;

// get resulting, previously sorted array
$array_sorted_asc = $sorter->collection();

// reverse order and return the resulting array
$array_sorted_desc = $sorter->desc('name')->collection();

Result for the above would be, (*3)

// $array_sorted_asc

Array
(
    [0] => Array
        (
            [name] => Arthur
        )

    [1] => Array
        (
            [name] => Mark
        )

    [2] => Array
        (
            [name] => Tom
        )

)

// $array_sorted_desc

Array
(
    [0] => Array
        (
            [name] => Tom
        )

    [1] => Array
        (
            [name] => Mark
        )

    [2] => Array
        (
            [name] => Arthur
        )

)

2. Multidimensional array

$array = [
    [
        'person' => [
            'name' => 'Arthur'
        ]
    ],
    [
        'person' => [
            'name' => 'Tom'
        ]
    ],
    [
        'person' => [
            'name' => 'Mark'
        ]
    ]
];

$sorter = new Sorter($array);

// sort ascending by child 'name' key
$array_sorted_asc = $sorter->asc(['person', 'name'])->collection();

// sort descending by child 'name' key
$array_sorted_desc = $sorter->desc(['person', 'name'])->collection();

Result, (*4)

// $array_sorted_asc

Array
(
    [0] => Array
        (
            [person] => Array
                (
                    [name] => Arthur
                )

        )

    [1] => Array
        (
            [person] => Array
                (
                    [name] => Mark
                )

        )

    [2] => Array
        (
            [person] => Array
                (
                    [name] => Tom
                )

        )

)

// $array_sorted_desc

Array
(
    [0] => Array
        (
            [person] => Array
                (
                    [name] => Tom
                )

        )

    [1] => Array
        (
            [person] => Array
                (
                    [name] => Mark
                )

        )

    [2] => Array
        (
            [person] => Array
                (
                    [name] => Arthur
                )

        )

)

3. Objects

For the purpose of this exercise let's assume we have 3 classes with 2 properties:, (*5)

  • name : string
  • wallet : Wallet

and Wallet class with one property:, (*6)

  • amount : int

First let's try and sort the 3 instances of the objects by their name property, (*7)

$collection[] = new Tutor(['name' => 'John']);
$collection[] = new Tutor(['name' => 'Craig']);

$collection[] = new Student(['name' => 'Marcus']);
$collection[] = new Student(['name' => 'George']);
$collection[] = new Student(['name' => 'Albert']);
$collection[] = new Student(['name' => 'Tony']);

$collection[] = new Staff(['name' => 'Greg']);
$collection[] = new Staff(['name' => 'Antony']);

$sorter = new Sorter($collection);

// sort ascending by child 'name' property
$array_sorted_asc = $sorter->asc('name')->collection();

// sort descending by child 'name' property
$array_sorted_desc = $sorter->desc('name')->collection();

Result for $array_sorted_asc, (*8)

Array
(
    [0] => SSD\Student Object
        (
            [name] => Albert
            [wallet] =>
        )

    [1] => SSD\Staff Object
        (
            [name] => Antony
            [wallet] =>
        )

    [2] => SSD\Tutor Object
        (
            [name] => Craig
            [wallet] =>
        )

    [3] => SSD\Student Object
        (
            [name] => George
            [wallet] =>
        )

    [4] => SSD\Staff Object
        (
            [name] => Greg
            [wallet] =>
        )

    [5] => SSD\Tutor Object
        (
            [name] => John
            [wallet] =>
        )

    [6] => SSD\Student Object
        (
            [name] => Marcus
            [wallet] =>
        )

    [7] => SSD\Student Object
        (
            [name] => Tony
            [wallet] =>
        )

)

and result for $array_sorted_desc, (*9)

Array
(
    [0] => SSD\Student Object
        (
            [name] => Tony
            [wallet] =>
        )

    [1] => SSD\Student Object
        (
            [name] => Marcus
            [wallet] =>
        )

    [2] => SSD\Tutor Object
        (
            [name] => John
            [wallet] =>
        )

    [3] => SSD\Staff Object
        (
            [name] => Greg
            [wallet] =>
        )

    [4] => SSD\Student Object
        (
            [name] => George
            [wallet] =>
        )

    [5] => SSD\Tutor Object
        (
            [name] => Craig
            [wallet] =>
        )

    [6] => SSD\Staff Object
        (
            [name] => Antony
            [wallet] =>
        )

    [7] => SSD\Student Object
        (
            [name] => Albert
            [wallet] =>
        )

)

4. Properties of the object property

Sorter allows you to sort objects based on the value associated with properties of the internal / embedded objects., (*10)

Our 3 classes have a property called wallet, which is of the Wallet object type. Let's try and sort our 3 objects using the property amount of the Wallet object., (*11)

$collection[] = new Tutor([
    'name' => 'John',
    'wallet' => new Wallet(['amount' => 10])
]);
$collection[] = new Tutor([
    'name' => 'Craig',
    'wallet' => new Wallet(['amount' => 12])
]);

$collection[] = new Student([
    'name' => 'Marcus',
    'wallet' => new Wallet(['amount' => 13])
]);
$collection[] = new Student([
    'name' => 'George',
    'wallet' => new Wallet(['amount' => 21])
]);
$collection[] = new Student([
    'name' => 'Albert',
    'wallet' => new Wallet(['amount' => 36])
]);
$collection[] = new Student([
    'name' => 'Tony',
    'wallet' => new Wallet(['amount' => 50])
]);

$collection[] = new Staff([
    'name' => 'Greg',
    'wallet' => new Wallet(['amount' => 22])
]);
$collection[] = new Staff([
    'name' => 'Antony',
    'wallet' => new Wallet(['amount' => 31])
]);

$sorter = new Sorter($collection);

// sort ascending by 'amount' property of the embedded object
$array_sorted_asc = $sorter->asc(['wallet', 'amount'])->collection();

// sort descending by 'amount' property of the embedded object
$array_sorted_desc = $sorter->desc(['wallet', 'amount'])->collection();

Result for $array_sorted_asc, (*12)

Array
(
    [0] => SSD\Tutor Object
        (
            [name] => John
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 10
                )

        )

    [1] => SSD\Tutor Object
        (
            [name] => Craig
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 12
                )

        )

    [2] => SSD\Student Object
        (
            [name] => Marcus
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 13
                )

        )

    [3] => SSD\Student Object
        (
            [name] => George
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 21
                )

        )

    [4] => SSD\Staff Object
        (
            [name] => Greg
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 22
                )

        )

    [5] => SSD\Staff Object
        (
            [name] => Antony
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 31
                )

        )

    [6] => SSD\Student Object
        (
            [name] => Albert
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 36
                )

        )

    [7] => SSD\Student Object
        (
            [name] => Tony
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 50
                )

        )

)

and result for $array_sorted_desc, (*13)

Array
(
    [0] => SSD\Student Object
        (
            [name] => Tony
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 50
                )

        )

    [1] => SSD\Student Object
        (
            [name] => Albert
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 36
                )

        )

    [2] => SSD\Staff Object
        (
            [name] => Antony
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 31
                )

        )

    [3] => SSD\Staff Object
        (
            [name] => Greg
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 22
                )

        )

    [4] => SSD\Student Object
        (
            [name] => George
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 21
                )

        )

    [5] => SSD\Student Object
        (
            [name] => Marcus
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 13
                )

        )

    [6] => SSD\Tutor Object
        (
            [name] => Craig
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 12
                )

        )

    [7] => SSD\Tutor Object
        (
            [name] => John
            [wallet] => SSD\Wallet Object
                (
                    [amount] => 10
                )

        )

)

The Versions

28/01 2016

dev-master

9999999-dev

Sort multidimensional arrays and objects.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Sebastian Sulinski

28/01 2016

v1.0.0

1.0.0.0

Sort multidimensional arrays and objects.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Sebastian Sulinski