# A List<T>.BinarySearch extension that takes a lambda expression

Published
Tags:

I was thinking the other day how inconvenient it is to use List<T>.BinarySearch if you don’t want to use the default comparer of T, needing to go and create a new class that implements IComparer<T>. Seems overly messy to require a whole new class just to do the binary search.

So I did a little research to see if there was a way around it and found this Jon Skeet answer (obviously) on StackOverflow. It gives a nice generic class that we can instantiate with a Comparison object and pass that into the BinarySearch method. I extended Jon’s answer to take a lambda expression instead, much like you can do with List.Sort.

public static class ListExtensions
{
public static int BinarySearch<T>(this List<T> list,
T item,
Func<T, T, int> compare)
{
return list.BinarySearch(item, new ComparisonComparer<T>(compare));
}
}

public class ComparisonComparer<T> : IComparer<T>
{

public ComparisonComparer(Func<T, T, int> compare)
{
if (compare == null)
{
throw new ArgumentNullException("comparison");
}
comparison = new Comparison<T>(compare);
}

public int Compare(T x, T y)
{
return comparison(x, y);
}
}

// Usage:

// ... Fill the list ...
list.Sort((a, b) => a.IntProp.CompareTo(b.IntProp));
var item = list[34];
int index = list.BinarySearch(item, (a, b) => a.IntProp.CompareTo(b.IntProp));
// index == 34


## Other handy extensions

### Evaluate equality without a second object

public static int BinarySearch<T>(this List<T> list, Func<T, int> compare)
where T : class
{
Func<T, T, int> newCompare = (a, b) => compare(a);
return list.BinarySearch((T)null, newCompare);
}

// Usage:
int i = list.BinarySearch(a => a.IntProp == 1);


### Return an object

public static T BinarySearchOrDefault<T>(this List<T> list,
T item,
Func<T, T, int> compare)
{
int i = list.BinarySearch(item, compare);
if (i >= 0)
return list[i];
return default(T);
}

// Usage:
SomeType obj = list.BinarySearchOrDefault(item,
(a, b) => a.IntProp.CompareTo(b.IntProp));


### Search for multiple matches

This extension will perform the binary search and then scan above and below the matching index for any other objects matching the compare Func. This function will run in O(1) best case (middle of list), O(\log n) average case and O(n) worst case (the entire list matches).

public static List<int> BinarySearchMultiple<T>(this List<T> list,
T item,
Func<T, T, int> compare)
{
var results = new List<int>();
int i = list.BinarySearch(item, compare);
if (i >= 0)
{
int below = i;
while (--below >= 0)
{
int belowIndex = compare(list[below], item);
if (belowIndex < 0)
break;
}

int above = i;
while (++above < list.Count)
{
int aboveIndex = compare(list[above], item);
if (aboveIndex > 0)
break;
}
}
return results;
}

// Usage:
List<SomeType> matches = list.BinarySearchMultiple(item,
(a, b) => a.IntProp.CompareTo(b.IntProp));
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.1838912069797516, "perplexity": 21329.544980424205}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-13/segments/1521257651007.67/warc/CC-MAIN-20180324210433-20180324230433-00625.warc.gz"}