搜索插件、代码仓与 APP 分类

插件详情v2.1.0

sorted

轻松对复杂对象列表进行排序。简单但功能强大的扩展方法。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
454
喜欢数
31
Pub 分数
140

安装插件

flutter pub add sorted

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

排序包为在 Dart 中对复杂对象列表进行排序提供了一种简单而强大的扩展方法。它通过直观的语法增强了内置的 List 类,支持按一个或多个字段进行升序和降序排序。非常适合处理用户列表、产品目录或 API 响应等结构化数据的开发者,可减少样板代码并提高可读性。该包轻量级、平台无关,并能无缝集成到 Android、iOS、Web、macOS、Windows 和 Linux 上的 Flutter 和 Dart 项目中。

适合场景

  • 按名称或年龄对用户数据进行排序
  • 按价格或评分排序产品列表
  • 组织API响应数组
  • 处理用户界面中的动态列表排序
  • 简化复杂的对象排序逻辑

主要特点

  • 易于使用的扩展方法
  • 支持多字段排序
  • 处理升序和降序
  • 最小化样板代码
  • 适用于任何对象类型
  • 跨平台兼容性

适合对象

  • 寻求简洁排序语法的开发者
  • 频繁排序列表的项目
  • 具有动态数据展示的 Flutter 应用
  • 注重代码可读性的团队
  • Dart 开发人员避免手动比较器逻辑

使用建议

  • 需要 Dart 2.12+
  • 字段名称没有运行时类型安全
  • 仅限基本排序模式
  • 不适用于高度复杂的排序规则
  • 不支持超出基本字段的自定义比较函数

原始 README

以下为关联源码仓库的 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.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。