https://github.com/albert221/sorted
Sort lists of complex objects with ease. Simple but powerful extension method.
https://github.com/albert221/sorted
dart-package
Last synced: 3 months ago
JSON representation
Sort lists of complex objects with ease. Simple but powerful extension method.
- Host: GitHub
- URL: https://github.com/albert221/sorted
- Owner: Albert221
- License: apache-2.0
- Created: 2020-10-23T16:01:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-08-01T12:35:50.000Z (almost 2 years ago)
- Last Synced: 2025-03-25T19:48:49.170Z (3 months ago)
- Topics: dart-package
- Language: Dart
- Homepage: https://pub.dev/packages/sorted
- Size: 153 KB
- Stars: 19
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# sorted
[![Package on pub.dev][pub-badge]][pub-link]
[![Test suite][tests-badge]][tests-link]Sort lists of complex objects with ease. Simple but powerful extension method.
```dart
auctions.sorted([
SortedOrdered(
(action) => auction.buyerContext.status,
[AuctionStatus.live, AuctionStatus.won, AuctionStatus.lost],
),
SortedComparable((auction) => auction.endedAt),
SortedComparable(
(auction) => auction.endedAt,
invert: true,
)
]);
```## Usage
Add the package to your `pubspec.yaml`:
```yaml
dependencies:
sorted:
```Import the package:
```dart
import 'package:sorted/sorted.dart';
```Call `sorted` extension method on any list and pass sorting rules of your choice. Order matters, so if first rule considers two items equal, next one will decide and so on.
## Sorting rules
All rules have an optional named argument `invert` which inverts the sorting order.
### `SortedOrdered`
```dart
SortedOrdered(
(auction) => auction.buyerContext.status,
[AuctionStatus.live, AuctionStatus.won, AuctionStatus.lost],
)
```Sorts items by the mapped property value in an order passed as a second argument. In this example, live auctions will be first, then won, and then lost.
### `SortedGroupOrdered`
```dart
SortedOrdered(
(road) => road.type,
[
[RoadType.nationalHighway, RoadType.stateHighway],
[RoadType.district],
[RoadType.dirt],
],
)
```Sorts items by the mapped property value in the order of order groups, but all values withing a certain group are considered equal.
### `SortedComparable`
```dart
SortedComparable((user) => user.lastName)
```Sorts items by the mapped property value using `Comparable.compare` which is basically `a.compareTo(b)`. Sorts strings alphabetically, numbers ascending, etc.
### `SortedComparator`
```dart
SortedComparator(
(auction) => auction.buyerContext.status,
(a, b) => /* your custom compare function */,
)
```Sorts items by the mapped property value using a custom comparator provided as a second argument.
[pub-link]: https://pub.dev/packages/sorted
[pub-badge]: https://img.shields.io/pub/v/sorted
[tests-link]: https://github.com/Albert221/sorted/actions?query=workflow%3ATest
[tests-badge]: https://img.shields.io/github/workflow/status/Albert221/sorted/Test