dragon-code/size-sorter

Easily sort clothing size, height, bra size, furniture size and more

1.5.0 2024-03-12 21:42 UTC

This package is auto-updated.

Last update: 2024-11-26 14:02:06 UTC


README

The Dragon Code Product size sorting

Stable Version Unstable Version Total Downloads Github Workflow Status License

Easily sort clothing size, height, bra size, furniture size and more

Installation

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"
    }
}

Compatibility

Note

This package can work outside the frameworks systems.

Usage

When calling a sort with common values, each element will be assigned to one of five groups:

  1. Letter clothing size (XXS, XS, M, L, etc.)
  2. Numerical size of clothes and shoes (9, 10, 44-46, 48, etc.)
  3. Bra size (70B, 75A, 80C, etc...)
  4. Overall dimensions of items (40x38x19 sm, etc.)
  5. 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.

Groups Order

By default, sizes are sorted by the following logical blocks:

  1. Letter clothing size (XXS, XS, M, L, etc.)
  2. Numerical size of clothes and shoes (9, 10, 44-46, 48, etc.)
  3. Bra size (70B, 75A, 80C, etc...)
  4. Overall dimensions of items (40x38x19 sm, etc.)
  5. 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

License

This package is licensed under the MIT License.