percentile method

T percentile(
  1. Iterable<T> iterable,
  2. num percentile, {
  3. bool isOrdered = false,
  4. T orElse()?,
})

Returns the value of the iterable at the given percentile between 0.0 and 1.0. Throws a RangeError if percentile is outside that range. To return the median, use a percentile of 0.5.

This implementation is most efficient on already sorted lists, as otherwise we have to copy and sort the collection first. You can avoid this expensive behavior by setting isOrdered to true.

Implementation

T percentile(Iterable<T> iterable, num percentile,
    {bool isOrdered = false, T Function()? orElse}) {
  if (!percentile.between(0, 1)) {
    throw RangeError.range(percentile, 0, 1);
  }
  if (iterable.isNotEmpty) {
    final ordered =
        isOrdered || this.isOrdered(iterable) ? iterable : sorted(iterable);
    final max = ordered.length - 1;
    final index = (percentile * max).round().clamp(0, max);
    return ordered.elementAt(index);
  }
  if (orElse == null) {
    throw StateError('Unable to compute percentile in $iterable.');
  }
  return orElse();
}