Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to perform a binary search on IList<T>?
    primarykey
    data
    text
    <p>Simple question - given an <code>IList&lt;T&gt;</code> how do you perform a binary search without writing the method yourself and without copying the data to a type with build-in binary search support. My current status is the following.</p> <ul> <li><code>List&lt;T&gt;.BinarySearch()</code> is not a member of <code>IList&lt;T&gt;</code></li> <li>There is no equivalent of the <a href="http://msdn.microsoft.com/en-us/library/system.collections.arraylist.adapter.aspx" rel="noreferrer"><code>ArrayList.Adapter()</code></a> method for <code>List&lt;T&gt;</code></li> <li><code>IList&lt;T&gt;</code> does not inherit from <code>IList</code>, hence using <code>ArrayList.Adapter()</code> is not possible</li> </ul> <p>I tend to believe that is not possible with build-in methods, but I cannot believe that such a basic method is missing from the BCL/FCL.</p> <p>If it is not possible, who can give the shortest, fastest, smartest, or most beatiful binary search implementation for <code>IList&lt;T&gt;</code>?</p> <p><strong>UPDATE</strong></p> <p>We all know that a list must be sorted before using binary search, hence you can assume that it is. But I assume (but did not verify) it is the same problem with sort - how do you sort <code>IList&lt;T&gt;</code>?</p> <p><strong>CONCLUSION</strong></p> <p>There seems to be no build-in binary search for <code>IList&lt;T&gt;</code>. One can use <code>First()</code> and <code>OrderBy()</code> LINQ methods to search and sort, but it will likly have a performance hit. Implementing it yourself (as an extension method) seems the best you can do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload