List< T>二进制搜索返回错误的值
本文关键字:返回 错误 搜索 二进制 List | 更新日期: 2023-09-27 18:13:33
二进制搜索返回错误的值,即使列表已排序。以下是列表:
1707 ABCD
1707 XXXX
1725 DEFG
1725 HIJK
1725 LMNOP
我从一个按时间(第一列)排列的文件中得到这个列表,所以我没有在代码中对它进行排序。当我对1725 DEFG进行二进制搜索时,它在按位补码之前返回1725 LMNOP。如果我做位补,结果将是1725 HIJK。
为什么?
实现如下:
public class RecComparer: IComparer<MyData>
{
public int Compare(MyData x, MyData y)
{
if (x.DateDetails == null)
{
if (y.DateDetails == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y.DateDetails == null)
{
return 1;
}
else
{
int retval = x.DateDetails.Length.CompareTo(y.DateDetails.Length);
if (retval != 0)
{
return retval;
}
else
{
return x.DateDetails.CompareTo(y.DateDetails);
}
}
}
}
}
下面是对BinarySearch的调用:
lookAhead = recordList.BinarySearch(lookAheadtime, (IComparer<MyData>)rc);
任何原因都可以解释为什么它会这样。
编辑:public class MyData
{
public string DateDetails { get; set; }
public string TrackDetails { get; set; }
}
更新#2:在有重复项的列表中使用自定义比较器进行二进制搜索。
假设,在"1707 ABCD"中,"1707"为DateDetails
,"ABCD"为TrackDetails
,则您有一个包含两个字段的数据数组,该数组已经按照的顺序排序了中的一个字段,即DateDetails
。因此,可能有多个条目具有相同的DateDetails
。
如果您执行BinarySearch,它将返回具有指定DateDetails
的条目之一,不一定是第一个。然后必须向后扫描以找到第一个。
public static class ListHelper
{
/// <summary>
/// Return the zero-based index of the first matching item in the sorted List,
/// if a match for item is found; otherwise, a negative number that is the bitwise
/// complement of the index of the next element that is larger than item or, if there is
/// no larger element, the bitwise complement of Count.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
/// <param name="comparer"></param>
/// <returns>The zero-based index of the first matching item in the sorted List,
/// if item is found; otherwise, a negative number that is the bitwise complement
/// of the index of the next element that is larger than item or, if there is
/// no larger element, the bitwise complement of Count.</returns>
public static int BinarySearchFirst<T>(this List<T> list, T item, IComparer<T> comparer)
{
int start = list.BinarySearch(item, comparer);
for (; start > 0 && comparer.Compare(list[start], list[start - 1]) == 0; start--)
;
return start;
}
/// <summary>
/// Find the zero-based indices of the first and last matching items in the sorted List,
/// if a match for item is found; otherwise, a negative number for both that is the bitwise
/// complement of the index of the next element that is larger than item or, if there is
/// no larger element, the bitwise complement of Count.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
/// <param name="comparer"></param>
/// <param name="start">The zero-based index of the first matching item in the List,
/// if item is found; otherwise, a negative number that is the bitwise complement
/// of the index of the next element that is larger than item or, if there is
/// no larger element, the bitwise complement of Count.</param>
/// <param name="end">The zero-based index of the first matching item in the sorted List,
/// if item is found; otherwise, a negative number that is the bitwise complement
/// of the index of the next element that is larger than item or, if there is
/// no larger element, the bitwise complement of Count.</param>
/// <returns>true if found, else false</returns>
public static bool BinarySearchRange<T>(this List<T> list, T item, IComparer<T> comparer, out int start, out int end)
{
start = end = list.BinarySearch(item, comparer);
if (start < 0)
return false;
for (; start > 0 && comparer.Compare(list[start], list[start - 1]) == 0; start--)
;
for (int last = list.Count - 1; end < last && comparer.Compare(list[end], list[end + 1]) == 0; end++)
;
return true;
}
}
我不确定我是否理解这个问题,但你不能使用linq来搜索列表吗?
recordList.Where(x=>x.columnName == 1725).First();