gpt4 book ai didi

C# List OrderBy float 成员

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:05 30 4
gpt4 key购买 nike

我有一个包含 A 类实例的列表。

class A
{
public int Id;
public float Value;
}

List<A> Collection = new List<A>( ... );

我想使用

对列表进行排序
Collection.OrderBy(item => item.Value);

这应该可以工作,但对于 float ,它会打乱顺序。会产生

1.0, 1.5, 1.6, 10.5, 11.54, 3.4, 4, 6.6, 7

其中 10.5、11.54 应位于列表底部。这种方法非常适用于 Value 是 int 的情况。有什么线索吗?

最佳答案

不创建新列表:

 Collection.Sort((x,y) => x.Value.CompareTo(y.Value));

关于C# List<T> OrderBy float 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24187287/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com