新生帖:一個用於字符串數組的智能排序類,用於相似度的對比!

        /// 
        /// 智能排序類
        /// 
        private class _Comparer : IComparer
        {
            public int Compare(string a, string b)
            {
                string x, y;
                if (a.Length >= b.Length)
                {
                    x = b;
                    y = a;
                }
                else
                {
                    x = a;
                    y = b;
                }

                int result = 0;

                if (x != y)
                {
                    List _x = new List(x.Length);

                    string s = x[0].ToString();
                    for (int i = 1; i < x.Length; i++)
                    {
                        if (Char.IsSeparator(x[i]))
                        {
                            _x.Add(s);
                            s = null;
                            i++;

                            if (i < x.Length)
                            {
                                s = x[i].ToString();
                            }
                        }
                        else
                        {
                            s += x[i].ToString();
                        }
                    }

                    if (s != null)
                    {
                        _x.Add(s);
                    }

                    foreach (string item in _x)
                    {
                        if (y.IndexOf(item) > -1 && ((float)(item.Length) / (float)(y.Length) > 0.5)) //0.5是相似度
                        {
                            return 0;
                        }
                    }

                    result = String.Compare(x, y);
               }

               return result;
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章