withSource method

Comparator<Object> withSource(
  1. OrderSourceProvider sourceProvider
)
inherited

Returns a new comparator that uses the given OrderSourceProvider to determine order metadata indirectly from an associated source.

This method enables advanced ordering scenarios where the order information is not directly available on the object but can be resolved through external metadata or relationships.

Parameters:

  • sourceProvider: The provider that resolves order sources for objects

Returns:

A new Comparator that incorporates the source provider logic

Example:

class AnnotationOrderSourceProvider implements OrderSourceProvider {
  @override
  Object? getOrderSource(Object obj) {
    if (obj is Class) {
      final orderAnnotation = obj.getAnnotation<Order>();
      return orderAnnotation?.value;
    }
    return null;
  }
}

final comparator = OrderComparator.INSTANCE.withSource(
  AnnotationOrderSourceProvider()
);

// Now classes can be sorted by their @Order annotation values
classes.sort(comparator);

Implementation

Comparator<Object> withSource(OrderSourceProvider sourceProvider) {
  return Comparator.comparingWith<Object, int>((o1) => doCompare(o1, null, sourceProvider), Comparator.naturalOrder());
}