FLUTTER ECOSYSTEM

Albert221/sorted

複雑なオブジェクトのリストを簡単に並べ替えます。シンプルだが強力な拡張メソッド。

並べ替え済み のプロジェクト画像
Stars
18
Forks
0
最終プッシュ(UTC)
2023/08/01
プロジェクト状態
公開中
Albert Wolszon GitHub avatar
GITHUB User

Albert Wolszon ↗

Flutter GDE. Senior Flutter developer. Likes Go. Speaks at conferences (sometimes). Private pilot.

@leancodeplWarsaw, Poland公式サイト ↗
言語Dart

技術トピック

このリポジトリが公開するパッケージ

使用している依存関係

依存関係一覧 5 件

元の README

以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。

README を開く / 閉じる

sorted

Package on pub.dev Test suite

Sort lists of complex objects with ease. Simple but powerful extension method.

auctions.sorted([
    SortedOrdered<Auction, AuctionStatus>(
        (action) => auction.buyerContext.status,
        [AuctionStatus.live, AuctionStatus.won, AuctionStatus.lost],
    ),
    SortedComparable<Auction, DateTime>((auction) => auction.endedAt),
    SortedComparable<Auction, DateTime>(
        (auction) => auction.endedAt,
        invert: true,
    )
]);

Usage

Add the package to your pubspec.yaml:

dependencies:
  sorted: <newest version>

Import the package:

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
SortedOrdered<Auction, AuctionStatus>(
  (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
SortedOrdered<Road, RoadType>(
  (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
SortedComparable<User, String>((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
SortedComparator<Auction, String>(
  (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.