我一直在研究C#中的Access文件编辑器,并且我一直在尝试将一个搜索功能添加到我的程序中。到目前为止,我有数据库文件填充一个二维数组,然后我用它来填充另一个窗口中的ListView框。在这个新窗口中,我希望能够按照型号搜索每个条目。到目前为止,我已经设法合并Levenstein算法,这似乎有很多用处。我可以使用算法来分配每个条目和搜索键盘之间的距离值,并将该值分配给另一个整数数组。我也可以按升序排列结果。使用Levenstein对字符串数组进行排序算法结果
但是,我目前的问题是,我希望将模型号码与莱文斯坦算法的距离值相同的方式排序,以便最相关的结果成为ListView框中的第一个选项。任何想法任何人??!?!
这里是我到目前为止有:
private void OnSearch(object sender, System.EventArgs e)
{
string a;
string b;
int[] result = new int[1000];
int[] sorted = new int[1000];
for (int i = 0; i < rowC; i++)
{
a = PartNum[i]; // Array to search
b = SearchBox1.Text; // keyword to search with
if (GetDistance(a, b) == 0)
{
return;
}
result[i] = GetDistance(a, b); //add each distance result into array
}
int index;
int x;
for (int j = 1; j < rowC; j++) //quick insertion sort
{
index = result[j];
x = j;
while ((x > 0) && (result[x - 1] > index))
{
result[x] = result[x - 1];
x = x - 1;
}
result[x] = index;
}
}
public static int GetDistance(string s, string t)
{
if (String.IsNullOrEmpty(s) || String.IsNullOrEmpty(t))
{
MessageBox.Show("Please enter something to search!!");
return 0;
}
int n = s.Length;
int m = t.Length;
if (n == 0)
{
return m;
}
else if (m == 0)
{
return n;
}
int[] p = new int[n + 1];
int[] d = new int[n + 1];
int[] _d;
char t_j;
int cost;
for (int i = 0; i <= n; i++)
{
p[i] = i;
}
for (int j = 1; j <= m; j++)
{
t_j = t[j - 1];
d[0] = j;
for (int i = 1; i <= n; i++)
{
cost = (s[i - 1] == t_j) ? 0 : 1;
d[i] = Math.Min(Math.Min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
}
_d = p;
p = d;
d = _d;
}
return p[n];
}
如何使用第二个选项并插入TakeWhile()?你最终有这样的事情: “...})。TakeWhile(i => i.distance!= 0).ToList();” 然后,您可以检查最后一个索引并使用它,或者之后进行排序。 – 2009-06-30 20:03:02