c# - 如何按相同权重的多个参数进行排序?

标签 c#

我正在尝试制作一个程序,通过多个参数对对象进行排序。 我需要 order by 对所有参数具有相同的权重。我需要使用哪些函数才能获得该结果?

我尝试使用 OrderBy() 然后 - ThenBy() 但它首先对第一个参数进行排序,因此排序不相等。

values = File.ReadAllLines(filepath)
              .Skip(1)
              .Select(v => Fund.FromCsv(v))
              .OrderByDescending(x => x.sharp)
              .ThenBy(x=>x.yearlychange)
              .ToList();

例如,您可以以股票市场为例,在这种情况下,我想按去年的 yield 对股票进行排序,但也要按标准差对股票进行排序。通过这种方式,我可以获得去年 yield 最高但标准差也最佳的股票。它不会是所有 yield 中最好的,它会被合并。

最佳答案

如您所知,这实际上并不是一个程序问题,更像是算法/领域问题。不过,如果您已经有了算法,您当然可以这样做。 (基于您在评论中提供的示例)

void Main()
{
    var funds = new List<Fund>();   
    funds.Add(new Fund() { Age = 18, Money = 100000 });
    funds.Add(new Fund() { Age = 20, Money = 101000 });

    //Here is normally your code with loading it from CSV
    //File.ReadAllLines(filepath)
    //        .Skip(1)
    //        .Select(v => Fund.FromCsv(v))

    var value = funds.OrderBy(t => t.SortingFactor);
}

public class Fund
{
    public int Age { get; set; }
    public decimal Money { get; set; }
    public decimal SortingFactor
    {
        get
        {
            //Here is your domain algorithm you must sort out first (your example data would be)
            return Money / Age;
        }
    }
}

关于c# - 如何按相同权重的多个参数进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58464850/

相关文章:

c# - TreeView 加减图标变大

c# - 打印未从页面的上边缘开始

c# - 当方法为 TaskCompletionSource<bool> 变量返回 .Task 时,这意味着什么?

c# - 如何取消并发的繁重任务?

c# - 在 C# 中通过 WCF 访问需要证书的 Web 服务 (HTTPS)

c# - 使用 Rijndael 加密时的 key 、盐和 IV

c# - 在 Unity 中更改按钮的文本

c# - 公共(public)证书在 WCF 中扮演什么角色?

c# - 有没有办法从 .Net 中创建 azure 服务总线命名空间?

c# - 释放在 WPF Application.OnStartUp() : Which thread owns it? 中创建的命名互斥体