the-jj/spl-collections-sort
Composer 安装命令:
composer require the-jj/spl-collections-sort
包简介
Helper library for sorting Spl data structures
README 文档
README
Several methods for sorting SPL datastructures. Currently only SplFixedArray objects are supported. Should be fast with big arrays as well.
Basic usage
All sorting algorithms accept SplFixedArray object as first argument and optional comparison callback function as second argument. Sorting is done in place.
Insertion sort
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::insertionSort($splFixedArray); var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
Custom comparison function
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::insertionSort($splFixedArray, function($a, $b) { if ($a < $b) return 1; if ($a > $b) return -1; return 0; }); var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
Boundaries
Additionally, insertion sort accepts two more arguments - boundaries $low and $high:
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::insertionSort($splFixedArray, null, 2, 4); var_dump($splFixedArray->toArray()); // [5, 3, 0, 6, 8, 4]
Quicksort
Non-recursive implementation is used, which makes it viable for sorting big arrays. Upon reaching subsets of 5 elements or less, falls back to insertion sort.
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::quickSort($splFixedArray); var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
Custom comparison function
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::quickSort($splFixedArray, function($a, $b) { if ($a < $b) return 1; if ($a > $b) return -1; return 0; }); var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
Mergesort
A bottom-up (non-recursive) implementation is used.
Usage? You guessed it:
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::mergeSort($splFixedArray); var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
Custom comparison function
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::mergeSort($splFixedArray, function($a, $b) { if ($a < $b) return 1; if ($a > $b) return -1; return 0; }); var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
Array sort
There is one more sorting method - array sort that uses PHP's sort() (or usort()) method. Usage is same as with the above algorithms:
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::arraySort($splFixedArray); var_dump($splFixedArray->toArray()); // [0, 3, 4, 5, 6, 8]
Custom comparison function
$splFixedArray = SplFixedArray::fromArray([5, 3, 8, 6, 0, 4]); SplFixedArraySort::arraySort($splFixedArray, function($a, $b) { if ($a < $b) return 1; if ($a > $b) return -1; return 0; }); var_dump($splFixedArray->toArray()); // [8, 6, 5, 4, 3, 0]
Why?
When using a regular PHP array, we are offered a vast array (no pun intended) of sorting functions. However, no such thing is offered for SplFixedArray objects in vanilla PHP.
This library is to change that. Currently it offers several methods for sorting SplFixedArray objects. In the future, I might add support for other structures (that make sense to be sorted, like doubly-linked list?).
Installation
composer require the-jj/spl-collections-sort
the-jj/spl-collections-sort 适用场景与选型建议
the-jj/spl-collections-sort 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 the-jj/spl-collections-sort 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 the-jj/spl-collections-sort 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-25