排序包为在 Dart 中对复杂对象列表进行排序提供了一种简单而强大的扩展方法。它通过直观的语法增强了内置的 List 类,支持按一个或多个字段进行升序和降序排序。非常适合处理用户列表、产品目录或 API 响应等结构化数据的开发者,可减少样板代码并提高可读性。该包轻量级、平台无关,并能无缝集成到 Android、iOS、Web、macOS、Windows 和 Linux 上的 Flutter 和 Dart 项目中。
适合场景
按名称或年龄对用户数据进行排序
按价格或评分排序产品列表
组织API响应数组
处理用户界面中的动态列表排序
简化复杂的对象排序逻辑
主要特点
易于使用的扩展方法
支持多字段排序
处理升序和降序
最小化样板代码
适用于任何对象类型
跨平台兼容性
适合对象
寻求简洁排序语法的开发者
频繁排序列表的项目
具有动态数据展示的 Flutter 应用
注重代码可读性的团队
Dart 开发人员避免手动比较器逻辑
使用建议
需要 Dart 2.12+
字段名称没有运行时类型安全
仅限基本排序模式
不适用于高度复杂的排序规则
不支持超出基本字段的自定义比较函数
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
sorted
Sort lists of complex objects with ease. Simple but powerful extension method.
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.
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.
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.