BinarySearch在二维列表中

vin*_*nsa 5 c#

我有维度列表:

List<List<string>> index_en_bg = new List<List<string>>();

   index_en_bg.Add(new List<string>() { word1, translation1 }); 
   index_en_bg.Add(new List<string>() { word2, translation2 }); 
   index_en_bg.Add(new List<string>() { word3, translation3 });
Run Code Online (Sandbox Code Playgroud)

我会通过第一列(单词)进行二分查找,如下所示:

int row = index_en_bg.BinarySearch(searchingstr);
Run Code Online (Sandbox Code Playgroud)

但它只适用于一维列表.在我的案例中,我如何将其扩展到二维列表?我不想Dictionary上课.

fvu*_*fvu 7

在这种情况下,您需要提供自己的客户IComparer实现比较器

public class Comparer: IComparer<IList<string>>
{
    public int Compare(IList<string> x, IList<string> y)
    {
        // base the comparison result on the first element in the respective lists
        // eg basically
        return x[0].CompareTo(y[0]);
    }
Run Code Online (Sandbox Code Playgroud)

你会这样称呼它,提供一个列表,其中只填写你正在搜索的字段.

int row = index_en_bg.BinarySearch(new List<string>() {searchingstr},new Comparer());
Run Code Online (Sandbox Code Playgroud)