PHP code example of dragon-code / size-sorter

1. Go to this page and download the library: Download dragon-code/size-sorter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dragon-code / size-sorter example snippets


use DragonCode\SizeSorter\SizeSorter;

return new SizeSorter([
    'XXL',
    '26',
    '28',
    'XL',
    'ONE SIZE',
    'XXS',
    '2',
    '54',
])->sort();

/*
 * Returns:
 * 
 * Illuminate\Support\Collection([
 *   'XXS',
 *   'XL',
 *   'XXL',
 *   '2',
 *   '26',
 *   '28',
 *   '54',
 *   'ONE SIZE',
 * ])
 */

use DragonCode\SizeSorter\SizeSorter;

// Laravel models collection
$items = Size::get();

return new SizeSorter($items)
    ->column('title')
    ->sort();

use DragonCode\SizeSorter\SizeSorter;

return SizeSorter::items([
    // ...
])->sort();

use DragonCode\SizeSorter\Enum\Group;
use DragonCode\SizeSorter\SizeSorter;

return new SizeSorter($items)
    ->orderBy([
        Group::BraSize,
        Group::OtherSizes,
        Group::OverallDimensions,
        Group::ClothesAndShoes,
        Group::VolumeGroup,
        Group::LetterClothingSize,
    ])
    ->sort();

use DragonCode\SizeSorter\Enum\Group;
use DragonCode\SizeSorter\SizeSorter;

return new SizeSorter($items)
    ->orderBy([
        Group::BraSize,
        Group::OtherSizes,
    ])
    ->sort();

use DragonCode\SizeSorter\SizeSorter;

return new SizeSorter($items)
    ->column('foo')
    ->sort();

use DragonCode\SizeSorter\SizeSorter;

$items = [
    [
        'foo' => [
            'bar' => [
                'baz' => 'Some value',
            ]
        ]
    ]
];

return new SizeSorter($items)
    ->column('foo.bar.baz')
    ->sort();

use DragonCode\SizeSorter\SizeSorter;

class Foo
{
    public function __construct(
        public int $number,
        public string $value1,
        public string $value2,
    ) {}
}

$items = [
    new Foo(1, 'first 1', 'first 2'),
    new Foo(2, 'second 1', 'second 2'),
];

return new SizeSorter($items)
    ->column(function (Foo $item) {
        return $item->number % 2 === 0
            ? $item->value1
            : $item->value2;
    })
    ->sort();

use DragonCode\SizeSorter\SizeSorter;

$items = [
    ['foo' => 'XS'],
    ['foo' => '2XS'],
    ['foo' => '3XL'],
];

return new SizeSorter($items)
    ->column(function (array $item) {
        return match ($item['foo']) {
            '2XS' => 'XXS',
            '3XL' => 'XXXL',
            default => $item['foo']
        };
    })
    ->sort();