C# 带两个参数的排序列表
我有个问题。我有一个函数,几乎在任何地方都可以使用C# 带两个参数的排序列表,c#,.net,list,sorting,C#,.net,List,Sorting,我有个问题。我有一个函数,几乎在任何地方都可以使用 public void Transfusion(PatientQueue patientQ, List<Event> scheduler) { Console.Write("\n### TRANSFUSION ###\nBefore Transfusion: " + BloodLevel); var p1 = patientQ.RemovePatient();
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);
}
让我们看看控制台。
看起来函数中的一切都正常,但是当我想继续调度程序时,函数中发生的事情就不起作用了。有人能帮我吗 听起来你想把列表分开,但要作为一个整体来管理。换句话说,您希望将类型单独排序,并将其放置在EventTime中,EventTime也单独排序。有一种方法可以做到这一点,我承认这可能是最好的方法,也可能不是最好的方法,就是将它们分别排序到一个列表中,然后将它们重新添加到一个可以管理的新排序列表中 我举了一个例子,我按姓名和密码对人进行分类。显然,对于这个例子来说,人物模型很糟糕,但这是我已经记下来的,所以我使用了它 在本例中,我首先按姓名对人员进行排序并引用它,然后按密码对人员进行排序并引用它。最后,我创建了一个新的人员列表,并根据排序后的姓名和密码将每个人员添加到列表中。如果这不是你正在寻找或要求的,请让我知道,我会删除答案
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查询也可以解决这个问题,但我不能全神贯注。请让我知道这是否是您要找的。按类型排序的结果,然后事件时间将是aUT:2 aUT:5 BS:3 QS:6 WT:4我已经解决了我的问题,我创建了返回列表对象的函数,现在一切正常。非常感谢你的帮助@MichaelPuckettII@TyRRRax它在函数中工作的原因是您正在返回一个新列表。运行linq查询时,即使返回到列表,也不会修改原始引用,只会修改新引用。您必须使用任何更新机制更新调度程序。
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; }
}
}