topshelfcraft/supersort
Composer 安装命令:
composer require topshelfcraft/supersort
包简介
...a super-duper sorting function for your Craft templates.
README 文档
README
...a super-duper sorting function for your Craft CMS templates.
A Top Shelf Craft creation
Michael Rog, Proprietor
What is SuperSort?
The SuperSort plugin provides a powerful Twig filter that helps with ordering an array of values — either using one of PHP's built-in methods to sort the actual values, or using custom "sort as" methods to arrange the array based on the rendered results of processing each member as a Twig object template.
Using SuperSort's "sort as" methods, you can sort an array of elements by any accessible object value — including values from Matrix blocks, attributes of related elements, math calculations, etc.
Usage
Installation
Visit the Plugin Store in your Craft control panel, search for SuperSort, and click to Install the plugin.
Or, if you're feeling frisky, you can install SuperSort via Composer:
composer require topshelfcraft/supersort
General usage
Use the supersort filter to sort an array of objects.
(You can apply supersort to any array or ElementQuery. A non-array-like source will be converted into a single-item array... which probably isn't very useful for sorting.)
Basic Sorting
The first parameter of the filter specifies the sort method.
{% set sortedSource = source | supersort('asort') %}
You can use any of PHP's built-in sort methods:
If you don't supply this first parameter, a plain vanilla asort will be applied:
{% set sortedSource = source | supersort %}
In many common cases — such as when you're sorting names, numbers, or filenames — the default 'asort' algorithm may produce unwanted results. In those cases, consider using the case-insensitive natural sorting algorithm ('natcasesort').
Advanced ("As") Sorting
You can also sort by one of SuperSort's custom methods:
'sortAs''rsortAs''natsortAs''natcasesortAs'
To use these methods, you will provide a second parameter, which is a Twig object template to render using each object in the array:
{% set upcomingEvents = upcomingEvents | supersort('sortAs', '{eventDates.first.date}') %}
{% set upcomingEvents = upcomingEvents | supersort('sortAs', '{{ object.eventDates.first.date }}') %}
(This "object template" syntax is the same as you might use to define a Dynamic Subfolder Path for an Assets field.
Sort Flags
You can optionally provide a third parameter — one of PHP's sort flags.
{% set source = source | supersort('sortAs', '{foo}', SORT_NUMERIC) %}
{% set source = source | supersort('asort', null, SORT_NUMERIC) %}
If you don't supply this third parameter, the default (SORT_REGULAR) flag is used.
Custom-Order Sorting via Archetype
The custom-order methods sort an array by custom order provided in an archetype.
'customOrderSort''customOrderKsort'
To use these methods, you will provide an archetype (exemplar) list via the comp parameter:
{% set upcomingEvents = upcomingEvents | supersort('customOrderSort', comp: ['Value1', 'Value2']) %}
{% set upcomingEvents = upcomingEvents | supersort('customOrderKsort', comp: ['key1', 'key2']) %}
Items which do not appear in the archetype list appear at end of the list, in their original relative order.
Using SuperSort as a PHP helper
If you'd like to use the superSort() method as a helper in another Craft plugin or component, you can access it in PHP directly from the Sorter service:
$result = Sorter::superSort($array, $method, $as, $sortFlag, $comp);
(Note: PHP's built-in sort methods sort an array in place and return a boolean representing success. The superSort() method, by contrast, returns the result array.)
Support
What are the system requirements?
Craft 5.0+ and PHP 8.0+
I found a bug.
Nah...
I triple-checked. It's a bug.
Well, alright. Please open a GitHub Issue, or submit a PR to the 5.x.dev branch.
Contributors:
- Plugin development: Michael Rog / @michaelrog
- Craft 3 port: TJ Draper / @tjdraperpro
- Icon: Ralf Schmitzer, via The Noun Project
topshelfcraft/supersort 适用场景与选型建议
topshelfcraft/supersort 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 88.07k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2018 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cms」 「array」 「sort」 「list」 「sorting」 「Craft」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 topshelfcraft/supersort 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 topshelfcraft/supersort 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 topshelfcraft/supersort 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table.
Syntactic sugar for PHP's sorting
Easily provide front-end sorting controls for SilverStripe lists
Plug-ins for DataTables
GraphQL authentication for your headless Craft CMS applications.
统计信息
- 总下载量: 88.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2018-02-03