列表排序麻烦

时间:2018-11-02 19:18:49

标签: c# .net list sorting

我有一个程序员列表:

programmers.Add(new Programmer("Jake", 1.9, 2000));
programmers.Add(new Programmer("Richard", 1.0, 1300)); 

,我需要以此创建一个新的排序程序员列表 value => 2000 / 1.9。(向上)

我不知道如何将int除以double并按此结果对programmers进行排序。你能帮我怎么做吗?

到目前为止,我已经尝试过:

var ProgrammersSorted = programmers.OrderBy((x,y) => x.DailyWage / y.Speed).ToList();

程序员课程:

public class Programmer
{
    public string Name { get; private set; }
    public double Speed { get; private set; }
    public int DailyWage { get; private set; }

    public Project Project { get; private set; }

    public string ProjectName
    {
        get
        {
            return Project?.Name ?? "No project assigned";
        }
    }

    public Programmer(string name, double speed, int dailyWage)
    {
        Name = name;
        Speed = speed;
        DailyWage = dailyWage;
    }
}

2 个答案:

答案 0 :(得分:5)

您非常亲密。试试看:

var programmersSorted = programmers.OrderBy(x => x.DailyWage / x.Speed).ToList();

答案 1 :(得分:1)

请尝试以下代码,我认为这可以解决您的问题。

        var programmers = new List<Programmer>
        {
         new Programmer("SS",12.3,2345),
         new Programmer("ADE",1.21,22345),
         new Programmer("AR",12.2,23445),
         new Programmer("NK",12.5,23455)
        };

        var progrmrs = programmers.OrderBy(t => t.DailyWage / t.Speed).ToList();
        Console.WriteLine("Name\t Speed\t DailyWage");
        foreach (var prgrm in progrmrs)
        {
            Console.WriteLine("{0}\t {1}\t {2}", prgrm.Name, prgrm.Speed, prgrm.DailyWage);
        }