【问题标题】:Sorting list with two arguments带有两个参数的排序列表
【发布时间】:2018-10-23 09:10:46
【问题描述】:

我有问题。我有一个函数(几乎无处不在)

       public void Transfusion(PatientQueue patientQ, List<Event> scheduler)
    {
        Console.Write("\n### TRANSFUSION ###\nBefore Transfusion: " + BloodLevel);
        var p1 = patientQ.RemovePatient();            // Take 1st patient from Queue and get him into var p1 
        Console.Write("\tNeed " + p1.BloodUnits + " blood units..");
        // Remodeling of scheduler, making UT units first in scheduler
        SortedByType(scheduler);
        Console.WriteLine("\n\nPOSORTOWANY SCHEDULER: \n\n");
        ShowScheduler(scheduler);
        for (int i = 0; i < p1.BloodUnits; i++)
        {
            BloodStorageList.RemoveAt(0); // Removes blood Unit from the System
            scheduler.RemoveAt(0); // Removes information about utilisation from Scheduler
        }
        Console.WriteLine("\n\nPO USUNIECIU: \n\n");
        ShowScheduler(scheduler);
        BloodLevel = BloodLevel - p1.BloodUnits; // Reduce BloodLevel
        Console.WriteLine("\tAfter Transfusion: " + BloodLevel);
        Sorted(scheduler);
    }

现在让我们关注调度程序和排序。我想使用 SortByType。这是我的功能(它工作正常)

        scheduler = scheduler.OrderBy(a => a.Type).ThenBy(a => a.EventTime).ToList();
        foreach (var schedul in scheduler) Console.WriteLine($"{schedul.Type} : {schedul.EventTime}");
        Console.Read();

那么让我们看看Console.CLICK HERE 看起来在函数中一切正常,但是当我想继续调度程序时,函数内部发生的事情不起作用。有人可以帮助我吗?

【问题讨论】:

  • 按类型和事件时间排序的结果将是aUT : 2 aUT : 5 BS : 3 QS : 6 WT : 4
  • 我已经找到了我的问题,我创建了返回 List 对象的函数,现在一切正常。非常感谢您的帮助。 @MichaelPuckettII
  • @TyRRRax 它在函数中起作用的原因是您要返回一个新列表。当您运行 linq 查询时,即使您返回到列表,您也不会修改原始的,而只是修改新的引用。您必须使用您拥有的任何更新机制来更新调度程序。

标签: c# .net list sorting


【解决方案1】:

听起来您希望必须将列表分开但作为一个列表一起管理。换句话说,您希望对 Type 进行单独排序,并将其放在 EventTime 上,EventTime 也是自行排序的。做到这一点的一种方法(这可能是也可能不是我承认的最好方法)是将它们分别排序到一个列表中,然后将它们添加回一个可以管理的新排序列表中。

我做了一个例子,我按姓名和密码对人进行排序。显然,Person 模型对于这个例子来说很糟糕,但这是我已经研究过的,所以我使用了它。

在此示例中,我首先按名称对人员进行排序并引用它,然后按密码对人员进行排序并引用它。最后,我制作了一个新的人员列表,并按排序后的名称和密码将每个人添加到列表中。如果这不是您要查找或要求的内容,请告诉我,我将删除答案。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Question_Answer_Console_App
{
    public class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var people = new List<Person>()
            {
                new Person() { Name = "Mathew", Password = "2345" },
                new Person() { Name = "Mathew", Password = "1234" },
                new Person() { Name = "John", Password = "5678" },
                new Person() { Name = "Mark", Password = "5678" },
                new Person() { Name = "Luke", Password = "0987" },
                new Person() { Name = "John", Password = "6534" }
            };

            var names = people.OrderBy(person => person.Name).Select(person => person.Name).ToList();
            var passwords = people.OrderBy(person => person.Password).Select(person => person.Password).ToList();
            var sortPeople = new List<Person>();
            for (int i = 0; i < names.Count(); i++) sortPeople.Add(new Person() { Name = names[i], Password = passwords[i] });  

            foreach (var person in sortPeople) Console.WriteLine($"{person.Name} : {person.Password}");
            Console.Read();
        }
    }
    public class Person
    {
        public string Name { get; set; }
        public string Password { get; set; }
    }
}

我真的觉得有一个 linq 查询也可以解决这个问题,但我无法理解它。如果这是您要找的,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多