Is there a standard library function for binary search on a sorted list in Dart?

Viewed 740

Is there a standard library method for binary search on a sorted list in Dart?

I've Googled and there seems to be one in flutter, but I can't find one in dart.

I also found this bug which mentions lowerBound() and binary search. It appears there used to be a collections/algorithms package but this appears deprecated and I can't see a replacement in collections.dart.

It isn't difficult to write one but I prefer to use standard library methods if possible.

1 Answers

The methods are there in the collection.dart package.

They can be a little hard to spot. If you scroll down they are in the Functions section and there is both binarySearch() and lowerBound()

It's important to notice this is package:collection, not dart:collection which also exists. But both are written by the Dart developers.

To import it use:

import 'package:collection/collection.dart';
Related