# comparing<T, U extends Comparable<U>> static method

Comparator<T> comparing<T, U extends Comparable<U>>(

1. U keyExtractor(
   1. T
   )

)

Creates a comparator based on a `Comparable` key extracted from each element.

Useful when sorting objects that have a comparable field but do not implement `Comparable`.

### Example

    class User {
      final int age;
      User(this.age);
    }

    final byAge = Comparator.comparing<User, int>((user) => user.age);
    final users = [User(40), User(20)];
    users.sort(byAge.compare);
    print(users.map((u) => u.age)); // [20, 40]

## Implementation

    static Comparator<T> comparing<T, U extends Comparable<U>>(U Function(T) keyExtractor) => _ComparingComparableComparator(keyExtractor);