How to perform a binary search on IList?

前端 未结 11 1388
执念已碎
执念已碎 2020-11-28 09:32

Simple question - given an IList how do you perform a binary search without writing the method yourself and without copying the data to a type with bui

11条回答
  •  清歌不尽
    2020-11-28 10:09

    If you can use .NET 3.5, you can use the build in Linq extension methods:

    using System.Linq;
    
    IList ls = ...;
    var orderedList = ls.OrderBy(x => x).ToList();
    orderedList.BinarySearch(...);
    

    However, this is really just a slightly different way of going about Andrew Hare's solution, and is only really helpful if you are searching multiple times off of the same ordered list.

提交回复
热议问题