开发者

C# Binary Search on 2 indexes

开发者 https://www.devze.com 2022-12-15 02:57 出处:网络
I have an object with attributes ; startIndex, endIndex I am able to do binary search based on startIndex by implementing the following :

I have an object with attributes ; startIndex, endIndex

I am able to do binary search based on startIndex by implementing the following :

        int IComparable.CompareTo(object obj)
        {
            Repeat r = (Repeat)obj;
            return this.startIndex.CompareTo(r.startIndex);
        }

However with the same Repeat Object I d like to do binary search also on the end index separately.

开发者_JS百科

How can i do this ?

Thanks.


If you're using the Array.BinarySearch() or List<T>.BinarySearch() methods, you can use the overload that takes an IComparer or IComparer<T>. Then you can implement the comparison semantics in a separate class and pass it in when you want to do a search.

0

精彩评论

暂无评论...
验证码 换一张
取 消