针对参数失败的自定义比较器

发布于 2024-12-04 03:48:03 字数 1436 浏览 1 评论 0原文

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

给出这个测试代码:

  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) 会导致“我的猫有跳蚤”位于列表顶部。

我想我一定在这里做了一些奇怪/奇怪的事情..这里有什么问题吗?或者有更好的方法来实现我想要做的事情吗?

谢谢!

I am trying to write a custom comparer to sort a list of search results based on similarity. I would like the term most like the entered search term to appear first in the list, followed by phrases that start with the search phrase, then all other values in alpha order.

Given this test code:

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

I'm trying to use this Comparer:

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);
    }

Calling list.Sort(new MatchComparer(searchTerm) results in 'my cat has fleas' at the top of the list.

I think I must be doing something odd/weird here .. Is something wrong here or is there a better approach to what I'm trying to do?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一世旳自豪 2024-12-11 03:48:03

的所有可能返回值

您没有首先使用 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
    } 
}

You aren't using the all of the possible return values for CompareTo

-1 == x first
0 == are equal
1 == y first

you want something more like this

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
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文