dart-lang / core

This repository is home to core Dart packages.
https://pub.dev/publishers/dart.dev
BSD 3-Clause "New" or "Revised" License
19 stars 7 forks source link

Cannot use int type with the sortedBy API #669

Closed etiennestuder closed 10 months ago

etiennestuder commented 10 months ago

Using Dart 3.2.4 and the collections package, and writing the code below...

['alpha','beta'].sortedBy((element) => element.length);

I get the error printed below:

Error: Inferred type argument 'int' doesn't conform to the bound 'Comparable<K>' of the type variable 'K' on 'IterableExtension|sortedBy'.
 - 'Comparable' is from 'dart:core'.
Try specifying type arguments explicitly so that they conform to the bounds.
    var xx = ['alpha','beta'].sortedBy((element) => element.length);

Does it mean that int does not implement Comparable? I did not know how to apply the advice given in the error message.

I'm not a Dart expert, and it is unclear to me whether sorting by a criteria of type int is not supported by design when using the sortedBy API, or if I'm doing something wrong, or if there is an issue in the library.

etiennestuder commented 10 months ago

I found the following issue and solution:

['alpha', 'beta'].sortedBy<num>((element) => element.length);