基本上我在这里要做的是创建自己的结构并通过获取用户输入来利用它,将其添加到列表中,然后以不同的方式(ID等)对其进行排序。
我认为我正确地制作了结构,但我不知道如何比较这两个学生实例,按 ID 对它们进行排序,然后将它们打印出来(按 ID 排序)到控制台。
有任何想法吗?我想我正朝着正确的方向前进。
namespace App26
{
public struct Student
{
public String first, last;
public double ID;
public Student(String first, String last, double ID)
{
this.first = first;
this.last = last;
this.ID = ID;
}
}
class IDCompare : IComparer<Student>
{
public int Compare(Student a, Student b)
{
return a.first.CompareTo(b.f);
}
}
class Program
{
static void Main(string[] args)
{
String firstname, lastname, first, last;
double num, IDnum;
//First person
Console.WriteLine("Please enter first name");
firstname = Console.ReadLine();
Console.WriteLine("Please enter last name");
lastname = Console.ReadLine();
Console.WriteLine("Please enter ID");
IDnum = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
//Second Person
Console.WriteLine("Please enter first name");
first = Console.ReadLine();
Console.WriteLine("Please enter last name");
last = Console.ReadLine();
Console.WriteLine("Please enter ID");
num = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
List<Student> list = new List<Student>();
Student person1 = new Student(firstname, lastname, IDnum);
//Student person2 = new Student(first, last, num);
list.Add(person1);
list.Add(person2);
list.Sort();
foreach (Student i in list)
Console.WriteLine(i);
}
}
}