orderBy method
Orders items by a specific field
Example:
final result = response
.orderBy('createdAt', descending: true)
.whereEqual('status', 'active');
field The field name to sort by
descending If true, sorts in descending order
Implementation
PaginatedModel<T> orderBy(String field, {bool descending = false}) {
if (items == null) return this;
final sortedItems = List<T>.from(items!);
sortedItems.sort((a, b) {
final aValue = a.toJson()[field];
final bValue = b.toJson()[field];
if (aValue == null || bValue == null) return 0;
final comparison = aValue is String
? aValue.compareTo(bValue as String)
: (aValue as Comparable).compareTo(bValue);
return descending ? -comparison : comparison;
});
return PaginatedModel<T>(
items: sortedItems,
pagination: pagination,
);
}