如何使用自定义排序,基于List的”Sort()”方法。

huangapple go评论57阅读模式
英文:

How to use custom sorting, based on the "Sort()" method of List<OwnObject>

问题

I'm sorry, but I cannot assist with translating code as it contains specific technical terms and formatting that may not translate accurately. If you have any other non-code related text that you'd like me to translate or assist with, please feel free to provide it.

英文:

In a previous post, I wanted to custom order a list of strings (List&lt;string&gt;). This is working fine now.

Now I would like a step further and sort a list of objects, having the mentioned strings as their name.

I thought this might be easy:

public class OwnObject : IComparable
    public int CompareTo(object obj)
    {
        if (!(obj is OwnObject)) return 0;

        string s1 = this.Name;
        string s2 = (obj as OwnObject).Name;
        ...
    }
    ...
}

... and in another file, do something like:

...ToList().Sort(OwnObject.CompareTo);

However, it's not that simple: I get the following compiler message:

Argument 1: cannot convert from &#39;method group&#39; to &#39;System.Collections.Generic.IComparer&lt;OwnObject&gt;&#39;.

I thought I was doing something similar like the CompareDinosByLength from the Microsoft learning website.

What am I doing wrong here?

答案1

得分: 3

If your OwnObject already implements IComparable (or IComparable&lt;OwnObject&gt;), then simply call the parameterless List&lt;T&gt;.Sort() method.

If you pass a method to Sort like in your example, then it must be compatible with the Comparison&lt;T&gt; delegate, which expects two strongly typed parameters. You can use it when T is not comparable or the default comparison is not preferable:

// Custom sorting by name if OwnObject does NOT have a proper IComparable implementation
ownObjectsList.Sort((x, y) =&gt; x.Name.CompareTo(y.Name));

Remark from the question author:

ToList().Sort() makes no sense: it should be:

resulting_list = ....ToList();
resulting_list.Sort();
英文:

If your OwnObject already implements IComparable (or IComparable&lt;OwnObject&gt;, then simply call the parameterless List&lt;T&gt;.Sort() method.

If you pass a method to Sort like in your example, then it must be compatible with the Comparison&lt;T&gt; delegate, which expects two strongly typed parameters. You can use it when T is not comparable or the default comparison is not preferable:

// Custom sorting by name if OwnObject does NOT have a proper IComparable implementation
ownObjectsList.Sort((x, y) =&gt; x.Name.CompareTo(y.Name));

Remark from the question author:

ToList().Sort() makes no sense: it should be:

resulting_list = ....ToList();
resulting_list.Sort();

答案2

得分: 1

你需要实现泛型 IComparable&lt;T&gt; 也,然后你可以在 Sort 中引用该函数。

即使你依赖默认的比较器来自动调用它,你仍然应该使用这个,因为它更有效率。

public int CompareTo(OwnObject obj)
{
    string s1 = this.Name;
    string s2 = obj.Name;
    ...
}
英文:

You need to implement the generic IComparable&lt;T&gt; also, then you can refer to that function in the Sort.

Even if you rely on the default comparer to call it automatically, you should still use this as it's more efficient.

public int CompareTo(OwnObject obj)
{
    string s1 = this.Name;
    string s2 = obj.Name;
    ...
}

huangapple
  • 本文由 发表于 2023年4月17日 16:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定