PHPackages                             creativecrafts/laravel-sort-collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. creativecrafts/laravel-sort-collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

creativecrafts/laravel-sort-collection
======================================

A simple package to sort query collections.

1.2.1(2y ago)3533[4 PRs](https://github.com/CreativeCrafts/laravel-sort-collection/pulls)MITPHPPHP ^8.2CI passing

Since Jan 19Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/CreativeCrafts/laravel-sort-collection)[ Packagist](https://packagist.org/packages/creativecrafts/laravel-sort-collection)[ Docs](https://github.com/creativecrafts/laravel-sort-collection)[ RSS](/packages/creativecrafts-laravel-sort-collection/feed)WikiDiscussions main Synced yesterday

READMEChangelog (7)Dependencies (13)Versions (14)Used By (0)

A simple package to sort query collections.
===========================================

[](#a-simple-package-to-sort-query-collections)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b064df54683c639d79899b6ef9e39f26b3648586123e0cf01f2d23a0ed760be6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63726561746976656372616674732f6c61726176656c2d736f72742d636f6c6c656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creativecrafts/laravel-sort-collection)[![GitHub Tests Action Status](https://camo.githubusercontent.com/d07747cad108c12cde4bdd8dbc57962bcd2ab1f372ba1520551048c4911da056/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63726561746976656372616674732f6c61726176656c2d736f72742d636f6c6c656374696f6e2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/creativecrafts/laravel-sort-collection/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7d4380ce1689ce6985767135d1a6ac2d0c86e4e8508db113a6648d9af415e2fe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63726561746976656372616674732f6c61726176656c2d736f72742d636f6c6c656374696f6e2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/creativecrafts/laravel-sort-collection/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/9e2771d385099630e9e404ed260761f09989f83b991f30991874d758e846abe6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63726561746976656372616674732f6c61726176656c2d736f72742d636f6c6c656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creativecrafts/laravel-sort-collection)

A simple handy package to sort query collections.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require creativecrafts/laravel-sort-collection
```

You can publish the config file with:

```
php artisan vendor:publish --tag="sort-collection-config"
```

This is the contents of the published config file:

```
return [
  /**
     * The default sort direction to use when sorting a collection.
     * supported: asc, desc
     */
    'sort_direction' => 'desc',
];
```

Optionally, you can publish the views using

Usage
-----

[](#usage)

```
use CreativeCrafts\SortCollection\Sort;
// simple collection example
$collection = collect([
     ['name' => 'John', 'age' => 30],
     ['name' => 'Jane', 'age' => 25],
     ['name' => 'Jack', 'age' => 40],
]);
$sortKey = 'age'; // string
$sortDirection = 'desc'; // string
$sortedCollection = Sort::collection($collection, $sortKey, $sortDirection);
// Sort direction is optional, it will use the default sort direction from the config file if not provided(by default it is desc)

// output:
[
          ['name' => 'John', 'age' => 40],
          ['name' => 'Jane', 'age' => 30],
          ['name' => 'Jack', 'age' => 25],
 ]

// eloquent example
// This is useful when you have encrypted fields in your model. Querying the model will decrypt the fields,
// then you can sort the collection using Sort::collection() method.
// Sort direction is optional, it will use the default sort direction from the config file if not provided(by default it is desc)

$query = User::query()
            ->select('name', 'age')
            ->get();

$sortKey = 'age'; // string
$sortDirection = 'asc'; // string
$sortedCollection = Sort::collection($collection, $sortKey, $sortDirection);

//output:
[
          ['name' => 'Jack', 'age' => 25],
          ['name' => 'Jane', 'age' => 30],
          ['name' => 'John', 'age' => 40],
 ]
```

```
You can also retrieve the default sort direction from the config file.
use CreativeCrafts\SortCollection\Sort;

Sort::getDefaultSortDirection()
```

Newly added methods
-------------------

[](#newly-added-methods)

### For a single encrypted column:

[](#for-a-single-encrypted-column)

```
use CreativeCrafts\SortCollection\Sort;

// In your controller or service
$users = Sort::encryptedColumn(User::query(), 'encrypted_email', 'asc');
```

### For multiple columns (some might be encrypted):

[](#for-multiple-columns-some-might-be-encrypted)

```
use CreativeCrafts\SortCollection\Sort;

// Get all users first
$users = User::all();

// Sort by multiple columns (last sort has the highest priority)
$sortedUsers = Sort::multipleColumns($users, [
    'name' => 'asc',
    'encrypted_email' => 'desc',
    'created_at' => 'desc'
]);
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Godspower Oduose](https://github.com/rockblings)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance47

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 75.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~81 days

Recently: every ~120 days

Total

7

Last Release

775d ago

Major Versions

0.1.1 → 1.1.12023-01-23

PHP version history (2 changes)0.0.1PHP ^8.1|^8.2

1.2.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b5e5ba42a2029e0f2fa45bd8d874c98599af2d2a1e77bff012ea07eeb0b65bc?d=identicon)[rockblings](/maintainers/rockblings)

---

Top Contributors

[![rockblings](https://avatars.githubusercontent.com/u/5190259?v=4)](https://github.com/rockblings "rockblings (34 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (11 commits)")

---

Tags

laravelcreativeCraftslaravel-sort-collectionsort collection

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/creativecrafts-laravel-sort-collection/health.svg)

```
[![Health](https://phpackages.com/badges/creativecrafts-laravel-sort-collection/health.svg)](https://phpackages.com/packages/creativecrafts-laravel-sort-collection)
```

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
