排序套件為在 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.