C# 在.NET中计算列表最小值/最大值的最短代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/250553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:56:28  来源:igfitidea点击:

Shortest code to calculate list min/max in .NET

c#.netalgorithm

提问by ripper234

I'd like something like

我想要类似的东西

int minIndex = list.FindMin(delegate (MyClass a, MyClass b) {returns a.CompareTo(b);});

Is there a builtin way to do this in .NET?

在 .NET 中是否有内置的方法来做到这一点?

采纳答案by Nicholas Mancuso

Try looking at these:

试试看这些:

Min

最小

Max

最大限度

As long as your class implements IComparable, all you have to do is:

只要你的类实现了 IComparable,你所要做的就是:

List<MyClass> list = new List();
//add whatever you need to add

MyClass min = list.Min();
MyClass max = list.Max();

回答by Rune Grimstad

Using Linq you have the Min() and Max() functions.

使用 Linq,您可以使用 Min() 和 Max() 函数。

So you can do list.AsQueryable().Min();

所以你可以做 list.AsQueryable().Min();

回答by Marc Gravell

You note that "I'm still in 2" - you might, then, want to look at LINQBridge. This is actually aimed at C# 3.0 and .NET 2.0, but you should be able to use it with C# 2.0 and .NET 2.0 - just you'll have to use the long-hand:

您注意到“我仍然在 2” - 那么您可能想要查看LINQBridge。这实际上是针对 C# 3.0 和 .NET 2.0,但您应该能够将它与 C# 2.0 和 .NET 2.0 一起使用 - 只是您必须使用长手:

MyClass min = Enumerable.Min(list),
        max = Enumerable.Max(list);

Of course, it will be easier if you can switch to C# 3.0 (still targetting .NET 2.0).

当然,如果您可以切换到 C# 3.0(仍以 .NET 2.0 为目标)会更容易。

And if LINQBridge isn't an option, you can implement it yourself:

如果 LINQBridge 不是一个选项,您可以自己实现它:

static void Main()
{
    int[] data = { 3, 5, 1, 5, 5 };
    int min = Min(data);
}
static T Min<T>(IEnumerable<T> values)
{
    return Min<T>(values, Comparer<T>.Default);
}
static T Min<T>(IEnumerable<T> values, IComparer<T> comparer)
{
    bool first = true;
    T result = default(T);
    foreach(T value in values) {
        if(first)
        {
            result = value;
            first = false;
        }
        else
        {
            if(comparer.Compare(result, value) > 0) 
            {
                result = value;
            }
        }
    }
    return result;
}

回答by Ryan Lundy

Well, if you can't use .NET 3.5, you could always sort the list and then return list[0]. It might not be the fastest way, but it's probably the shortest code, especially if your class already implements IComparable.

好吧,如果您不能使用 .NET 3.5,您总是可以对列表进行排序,然后返回 list[0]。它可能不是最快的方法,但它可能是最短的代码,特别是如果您的类已经实现了 IComparable。

List<SomeClass> list = new List<SomeClass>();
// populate the list
// assume that SomeClass implements IComparable
list.Sort();
return list[0];               // min, or
return list[list.Count - 1];  // max

This also assumes, of course, that it doesn't matter which item you return if you have multiple items that are the minimum or maximum.

当然,这也假设如果您有多个最小或最大项目,则返回哪个项目并不重要。

If your class doesn't implement IComparable, you can pass in an anonymous delegate, something like this:

如果您的类没有实现 IComparable,您可以传入一个匿名委托,如下所示:

list.Sort(delegate(SomeClass x, SomeClass y) { return string.Compare(x.Name, y.Name); });