Easily sort clothing size, height, bra size, furniture size and more
To get the latest version of Product Size Sorter
, simply require the project using Composer:
composer require dragon-code/size-sorter
Or manually update require
block of composer.json
and run composer update.
{
"require": {
"dragon-code/size-sorter": "^1.0"
}
}
Note
This package can work outside the frameworks systems.
Compatibility | Versions |
---|---|
PHP | ^8.1 |
Laravel | ^8.0, ^9.0, ^10.0 |
Symfony | ^5.3, ^6.0 |
When calling a sort with common values, each element will be assigned to one of five groups:
- Letter clothing size (XXS, XS, M, L, etc.)
- Numerical size of clothes and shoes (9, 10, 44-46, 48, etc.)
- Bra size (70B, 75A, 80C, etc...)
- Overall dimensions of items (40x38x19 sm, etc.)
- Other values
use DragonCode\SizeSorter\Sorter;
return Sorter::sort(collect([
'XXL',
'26',
'28',
'XL',
'ONE SIZE',
'XXS',
'2',
'54',
]));
/*
* Returns:
*
* Collection([
* 'XXS',
* 'XL',
* 'XXL',
* '2',
* '26',
* '28',
* '54',
* 'ONE SIZE',
* ])
*/
use DragonCode\SizeSorter\Sorter;
$items = Size::query()->get();
return Sorter::sort($items, 'title');
You can see more examples in the test file.
By default, sizes are sorted by the following logical blocks:
- Letter clothing size (XXS, XS, M, L, etc.)
- Numerical size of clothes and shoes (9, 10, 44-46, 48, etc.)
- Bra size (70B, 75A, 80C, etc...)
- Overall dimensions of items (40x38x19 sm, etc.)
- Other values
But you can change the order by specifying identifiers as the third parameter:
use DragonCode\SizeSorter\Enum\Group;
use DragonCode\SizeSorter\Sorter;
return Sorter::sort($items, groupsOrder: [3, 5, 4, 2, 1]);
// or
return Sorter::sort($items, groupsOrder: [Group::GROUP_3, Group::GROUP_5, Group::GROUP_4, Group::GROUP_2, Group::GROUP_1]);
The final array will be formed in the specified order:
3 - 5 - 4 - 2 - 1
You can also specify some groups. For example:
use DragonCode\SizeSorter\Enum\Group;
use DragonCode\SizeSorter\Sorter;
return Sorter::sort($items, groupsOrder: [3, 5]);
// or
return Sorter::sort($items, groupsOrder: [Group::GROUP_3, Group::GROUP_5]);
In this case, the first two logical groups will be sorted in the specified order, and the subsequent ones will be in ascending order:
3 - 5 - 1 - 2 - 4
This package is licensed under the MIT License.