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

1

6

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.

Dinitrobenzene answered 30/5, 2020 at 19:51 Comment(0)
D
8

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';
Dinitrobenzene answered 30/5, 2020 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.