private  void AA(string[] arr,string key){...}
比如arr={"河北","河东区","河西区","山东"}   key="河东区",
那么要求得到的排序结果是{"河东区","河西区","河北","山东"}
总之,给出一组要排序的汉字(或英文)和一个作为排序依据的汉字(或英文),让你排序,跟排序依据最相符的元素排在最前.

解决方案 »

  1.   

    随便做了一个,看看行不行
            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);
                });
            }
      

  2.   

    修正一下: 
                        if (s == null)
                            return int.MaxValue;
      

  3.   

    ...改成1.1版的了,具体的匹配加权根据实际情况再调整,这里仅仅是一个大概的
            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        }