Сортировка списка List - C# (188773)
Формулировка задачи:
Помогите, пожалуйста, решить проблему с сортировкой.
Есть набор сущностей Человек, Ученик, Преподаватель, Студент, Аспирант, Рабочий. В программе мы задаём для них их свойства —
Общие: Имя, Фамилия, Отчество, Адрес
Личные: Название школы и Класс для Ученика; Название предмета для Преподавателя; Специальность для Рабочего и т.д.
В конце мы выводим список в таком порядке, в котором его и задавали (На скриншоте видно, что выводятся элементы в том порядке, в котором и записывались.), но теперь же мне нужно сделать сортировку по сущностям (Ученик, Студент и т.д.), но я не пойму, как это сделать, ведь эти сущности у меня записаны, как классы, они не подписаны в самом списке... Как отсортировать? Помогите, пожалуйста.
P.S. То, что закомментировано — это я пробовал в каждую строку списка записывать без ввода тип сущности, которой принадлежит строка, и по этому параметру сортировать, но не вышло...
P.P.S. List.Sort() пробовал, но, видимо, я слишком кривой — не вышло.
using System;
using System.Collections.Generic;
namespace Лаба_10_часть_3
{
interface IPerson
{
//string Type { get; set; }
string Name { get; set; }
string Surname { get; set; }
string Otchestvo { get; set; }
string Adress { get; set; }
}
interface IPupil
{
string NameSchool { get; set; }
string NameKlass { get; set; }
}
interface ITeacher
{
string NamePredmet { get; set; }
}
interface IStudent
{
string NameUniver { get; set; }
string NomerStud { get; set; }
}
interface IWorker
{
string Special { get; set; }
}
interface IAdvancedStudent
{
string TemaDissertazii { get; set; }
}
class CPupil : IPupil, IPerson
{
//public string Type { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Otchestvo { get; set; }
public string Adress { get; set; }
public string NameSchool { get; set; }
public string NameKlass { get; set; }
public override string ToString()
{
return "Ученик — " + " Имя: " + Name + " Фамилия: " + Surname + " Отчество: " + Otchestvo + " Адрес: " + Adress + " Номер школы" + NameSchool + " Номер класса: " + NameKlass;
}
}
class CTeacher : ITeacher, IPerson
{
//public string Type { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Otchestvo { get; set; }
public string Adress { get; set; }
public string NamePredmet { get; set; }
public override string ToString()
{
return "Учитель — " + " Имя: " + Name + " Фамилия: " + Surname + " Отчество: " + Otchestvo + " Адрес: " + Adress + " Название предмета: " + NamePredmet;
}
}
class CStudent : IStudent, IPerson
{
//public string Type { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Otchestvo { get; set; }
public string Adress { get; set; }
public string NameUniver { get; set; }
public string NomerStud { get; set; }
public override string ToString()
{
return "Студент — " + " Имя: " + Name + " Фамилия: " + Surname + " Отчество: " + Otchestvo + " Адрес: " + Adress + " Название университета: " + NameUniver + " Номер студента: " + NomerStud;
}
}
class CWorker : IWorker, IPerson
{
//public string Type { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Otchestvo { get; set; }
public string Adress { get; set; }
public string Special { get; set; }
public override string ToString()
{
return "Рабочий — " + " Имя: " + Name + " Фамилия: " + Surname + " Отчество: " + Otchestvo + " Адрес: " + Adress + " Специальность: " + Special;
}
}
class CAdvanceStudent : IAdvancedStudent, IStudent, IPerson
{
//public string Type { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Otchestvo { get; set; }
public string Adress { get; set; }
public string NameUniver { get; set; }
public string NomerStud { get; set; }
public string TemaDissertazii { get; set; }
public override string ToString()
{
return "Аспирант — " + " Имя: " + Name + " Фамилия: " + Surname + " Отчество: " + Otchestvo + " Адрес: " + Adress + " Название университета: " + NameUniver + " Номер студента: " + NomerStud + " Тема диссертации: " + TemaDissertazii;
}
}
class List
{
public static List<IPerson> spisok_chel = new List<IPerson>();
}
class Program
{
static void Main(string[] args)
{
string control = "";
while (control != "exit")
{
Console.WriteLine("***********************************");
Console.WriteLine("** Создание сущности: **");
Console.WriteLine("***********************************");
Console.WriteLine("** 1 - Создать ученика **");
Console.WriteLine("** 2 - Создать учителя **");
Console.WriteLine("** 3 - Создать аспиранта **");
Console.WriteLine("** 4 - Создать студента **");
Console.WriteLine("** 5 - Создать рабочего **");
Console.WriteLine("** 6 - Вывести список сущностей **");
Console.WriteLine("** 7 - Выход **");
Console.WriteLine("***********************************");
control = Console.ReadLine();
Console.Clear();
switch (control)
{
case "1":
CPupil cp = new CPupil();
//cp.Type = "Pupil";
Console.Write("Введите имя: ");
cp.Name = Console.ReadLine();
Console.Write("Введите фамилию: ");
cp.Surname = Console.ReadLine();
Console.Write("Введите отчество: ");
cp.Otchestvo = Console.ReadLine();
Console.Write("Введите адрес: ");
cp.Adress = Console.ReadLine();
Console.Write("Введите номер класса: ");
cp.NameKlass = Console.ReadLine();
Console.Write("Введите номер школы: ");
cp.NameSchool = Console.ReadLine();
List.spisok_chel.Add(cp);
Console.ReadKey();
Console.Clear();
break;
case "2":
CTeacher ct = new CTeacher();
//ct.Type = "Teacher";
Console.Write("Введите имя: ");
ct.Name = Console.ReadLine();
Console.Write("Введите фамилию: ");
ct.Surname = Console.ReadLine();
Console.Write("Введите отчество: ");
ct.Otchestvo = Console.ReadLine();
Console.Write("Введите адрес: ");
ct.Adress = Console.ReadLine();
Console.Write("Введите название преподаваемого предмета: ");
ct.NamePredmet = Console.ReadLine();
List.spisok_chel.Add(ct);
Console.ReadKey();
Console.Clear();
break;
case "3":
CAdvanceStudent cas = new CAdvanceStudent();
//cas.Type = "AStudent";
Console.Write("Введите имя: ");
cas.Name = Console.ReadLine();
Console.Write("Введите фамилию: ");
cas.Surname = Console.ReadLine();
Console.Write("Введите отчество: ");
cas.Otchestvo = Console.ReadLine();
Console.Write("Введите адрес: ");
cas.Adress = Console.ReadLine();
Console.Write("Введите название университета: ");
cas.NameUniver = Console.ReadLine();
Console.Write("Введите номер студента: ");
cas.NomerStud = Console.ReadLine();
Console.Write("Введите тему диссертации: ");
cas.TemaDissertazii = Console.ReadLine();
List.spisok_chel.Add(cas);
Console.ReadKey();
Console.Clear();
break;
case "4":
CStudent cs = new CStudent();
//cs.Type = "Student";
Console.Write("Введите имя: ");
cs.Name = Console.ReadLine();
Console.Write("Введите фамилию: ");
cs.Surname = Console.ReadLine();
Console.Write("Введите отчество: ");
cs.Otchestvo = Console.ReadLine();
Console.Write("Введите адрес: ");
cs.Adress = Console.ReadLine();
Console.Write("Введите название университета ");
cs.NameUniver = Console.ReadLine();
Console.Write("Введите номер студента: ");
cs.NomerStud = Console.ReadLine();
List.spisok_chel.Add(cs);
Console.ReadKey();
Console.Clear();
break;
case "5":
CWorker cw = new CWorker();
//cw.Type = "Worker";
Console.Write("Введите имя: ");
cw.Name = Console.ReadLine();
Console.Write("Введите фамилию: ");
cw.Surname = Console.ReadLine();
Console.Write("Введите отчество: ");
cw.Otchestvo = Console.ReadLine();
Console.Write("Введите адрес: ");
cw.Adress = Console.ReadLine();
Console.Write("Введите специальность ");
cw.Special = Console.ReadLine();
List.spisok_chel.Add(cw);
Console.ReadKey();
Console.Clear();
break;
case "6":
//spisok_chel.Sort(IPerson.Type);
foreach (IPerson item in List.spisok_chel)
{
//if (spisok_chel.EndsWith("T"))
Console.WriteLine(item.ToString());
Console.WriteLine("*********************************************************");
}
break;
case "7":
{
control = "exit";
}
Console.ReadKey();
Console.Clear();
break;
}
}
}
}
}Решение задачи: «Сортировка списка List»
textual
Листинг программы
//using System.Linq;
case "6":
List.spisok_chel.Sort((a, b) => a.GetType().Name.CompareTo(b.GetType().Name));
foreach (var item in List.spisok_chel)
{
Console.WriteLine(item);
}
break;