An open API service indexing awesome lists of open source software.

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.

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