【问题标题】:Custom Comparer against a parameter failing针对参数失败的自定义比较器
【发布时间】:2011-09-12 23:30:10
【问题描述】:

我正在尝试编写一个自定义比较器来根据相似性对搜索结果列表进行排序。我希望与输入的搜索词最相似的词首先出现在列表中,然后是以搜索词组开头的词组,然后是按字母顺序排列的所有其他值。

鉴于此测试代码:

  string searchTerm = "fleas";
  List<string> list = new List<string>
                                       {
                                           "cat fleas",
                                           "dog fleas",
                                           "advantage fleas",
                                           "my cat has fleas",
                                           "fleas",
                                           "fleas on my cat"
                                       };

我正在尝试使用这个比较器:

public class MatchComparer : IComparer<string>
{
    private readonly string _searchTerm;

    public MatchComparer(string searchTerm)
    {
        _searchTerm = searchTerm;
    }

    public int Compare(string x, string y)
    {
        if (x.Equals(_searchTerm) ||
             y.Equals(_searchTerm))
            return 0;

        if (x.StartsWith(_searchTerm) ||
            y.StartsWith(_searchTerm))
            return 0;

        if (x.Contains(_searchTerm))
            return 1;

        if (y.Contains(_searchTerm))
            return 1;

        return x.CompareTo(y);
    }

调用 list.Sort(new MatchComparer(searchTerm) 会导致列表顶部的“我的猫有跳蚤”。

我想我一定是在做一些奇怪/奇怪的事情......这里有什么问题还是有更好的方法来解决我正在尝试做的事情?

谢谢!

【问题讨论】:

    标签: c# linq contains compareto icomparer


    【解决方案1】:

    您没有使用 CompareTo 的所有可能返回值

    -1 == x 先 0 == 相等 1 == y 优先

    你想要更多这样的东西

    public class MatchComparer : IComparer<string> 
    { 
        private readonly string _searchTerm; 
    
        public MatchComparer(string searchTerm) 
        { 
            _searchTerm = searchTerm; 
        } 
    
        public int Compare(string x, string y) 
        { 
            if (x.Equals(y)) return 0; // Both entries are equal;
            if (x.Equals(_searchTerm)) return -1; // first string is search term so must come first
            if (y.Equals(_searchTerm)) return 1; // second string is search term so must come first
            if (x.StartsWith(_searchTerm)) {
                // first string starts with search term
                // if second string also starts with search term sort alphabetically else first string first
                return (y.StartsWith(_searchTerm)) ? x.CompareTo(y) : -1;
            }; 
            if (y.StartsWith(_searchTerm)) return 1; // second string starts with search term so comes first
            if (x.Contains(_searchTerm)) {
                // first string contains search term
                // if second string also contains the search term sort alphabetically else first string first
                return (y.Contains(_searchTerm)) ? x.CompareTo(y) : -1; 
            }
            if (y.Contains(_searchTerm)) return 1; // second string contains search term so comes first
            return x.CompareTo(y); // fall back on alphabetic
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多