private void AA(string[] arr,string key){...}
比如arr={"河北","河东区","河西区","山东"} key="河东区",
那么要求得到的排序结果是{"河东区","河西区","河北","山东"}
总之,给出一组要排序的汉字(或英文)和一个作为排序依据的汉字(或英文),让你排序,跟排序依据最相符的元素排在最前.
比如arr={"河北","河东区","河西区","山东"} key="河东区",
那么要求得到的排序结果是{"河东区","河西区","河北","山东"}
总之,给出一组要排序的汉字(或英文)和一个作为排序依据的汉字(或英文),让你排序,跟排序依据最相符的元素排在最前.
解决方案 »
- c#下载程序在win7上的代理问题
- GridView中Columns列宽问题
- C# 读取其他系统注册表问题
- 先后建两个窗口,但从构造函数的得到的变量内容会被同时至更新 为什么呢?难道站指针?
- 【没有注册类别】32位系统运行正常的系统,在64位系统上运行不正常
- 如何在知道checkbox名称的情况下,将其转换为checkbox控件?
- 三层开发的问题,非常简单。 高手指点 大恩不言谢。
- 急!哥哥们,如何手动控制SQL SERVER 2000中的事务加锁级别问题
- [重要提醒]提问或回复的时候请注意,您的代码中最好不要包括用户名、密码等重要信息
- PropertyGrid组件 弹出一个Form 紧急求助
- delegate--小女子请教各位大虾:)
- 能不能通过调用sqlDataAdapter.Update()来更新视图中源表的数据
private static void Sort(string[] array, string key)
{
if (array == null)
throw new ArgumentNullException("array");
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
Array.Sort<string>(array, delegate(string x, string y)
{
Converter<string, int> getDismatchLevel = delegate(string s)
{
int result = 0;
if (s == key)
return 0;
if (s == null)
return int.MinValue;
int minLength = Math.Min(s.Length, key.Length);
for (int i = 0; i < minLength; i++)
if (s[i] != key[i])
result += minLength - i;
int lengthDiff = Math.Abs(s.Length - key.Length);
result += lengthDiff;
return result;
};
return getDismatchLevel(x) - getDismatchLevel(y);
});
}
if (s == null)
return int.MaxValue;
static void Main(string[] args)
{
string[] array = new string[] { "河北", "河东区", "河西区", "山东" };
string key = "河东区";
Array.Sort(array, new CustomComparer(key));
foreach (string s in array)
{
Console.WriteLine(s);
}
Console.ReadLine();
} internal class CustomComparer
: IComparer
{ #region Fields
private string key;
#endregion #region Ctors public CustomComparer(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
this.key = key;
} #endregion #region IComparer Members public int Compare(object x, object y)
{
return Compare(x as string, y as string);
} #endregion #region Private Methods private int Compare(string x, string y)
{
return GetDismatchLevel(x) - GetDismatchLevel(y);
} private int GetDismatchLevel(string s)
{
int result = 0;
if (s == key)
return 0;
if (s == null)
return int.MaxValue;
int minLength = Math.Min(s.Length, key.Length);
for (int i = 0; i < minLength; i++)
if (s[i] != key[i])
result += minLength - i;//根据需要修改匹配加权
int lengthDiff = Math.Abs(s.Length - key.Length);
result += lengthDiff;//根据需要修改匹配加权
return result;
} #endregion }