Сортировка в порядке убывания по указанному отделу - C#
Формулировка задачи:
Нужно сделать сортировку по убыванию переменной Term
Код:
using System;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student[] array = new Student[8];
using (StreamReader reader = new StreamReader("C:\\filex.txt", Encoding.Default))
{
for (int i = 0; i < 8; i++)
{
array[i] = new Student(reader.ReadLine().Split('|'));
}
}
Console.Write("1.Вывести информацию о всех сотрудниках \n2.Вывести информацию о сотрудниках определенного отдела\n\nВыберите пункт: ");
switch (Console.ReadLine())
{
case "1":
Console.Clear();
Console.WriteLine("Информация о всех студентах: ");
foreach (Student s in array)
s.Show();
break;
case "2":
Console.Clear();
Console.Write("Введите номер отдела: ");
string fac = Console.ReadLine();
if (!Student.CheckFac(fac, array))
Console.WriteLine("Такого отдела нет!");
else
foreach (Student s in array)
if (s.Faculty == fac)
s.Show();
break;
}
Console.ReadKey();
}
}
struct Student
{
public string FIO;
public string Job;
public string Faculty;
public int Term;
public Student(string[] args)
{
FIO = args[0];
Job = args[1];
Faculty = args[2];
Term = int.Parse(args[3]);
}
static public bool CheckFac(string fac, Student[] array)
{
bool temp = false;
foreach (Student s in array)
if (s.Faculty == fac)
temp = true;
return temp;
}
public void Show()
{
Console.WriteLine("\nФИО: {0} \nДолжность: {1} \nНомер отдела: {2} \nСтаж: {3}", FIO, Job, Faculty, Term);
}
}
}Решение задачи: «Сортировка в порядке убывания по указанному отделу»
textual
Листинг программы
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student[] array = new Student[8];
using (StreamReader reader = new StreamReader("C:\\filex.txt", Encoding.Default))
{
for (int i = 0; i < 8; i++)
{
array[i] = new Student(reader.ReadLine().Split('|'));
}
}
List<Student> list = new List<Student>();
foreach (var i in array)
{
list.Add(i);
}
list.Sort();
Console.Write("1.Вывести информацию о всех сотрудниках \n2.Вывести информацию о сотрудниках определенного отдела\n\nВыберите пункт: ");
switch (Console.ReadLine())
{
case "1":
Console.Clear();
Console.WriteLine("Информация о всех студентах: ");
foreach (Student s in array)
s.Show();
break;
case "2":
Console.Clear();
Console.Write("Введите номер отдела: ");
string fac = Console.ReadLine();
if (!Student.CheckFac(fac, array))
Console.WriteLine("Такого отдела нет!");
else
foreach (Student s in array)
if (s.Faculty == fac)
s.Show();
break;
}
Console.ReadKey();
}
}
struct Student : IComparable<Student>
{
public string FIO;
public string Job;
public string Faculty;
public int Term;
public Student(string[] args)
{
FIO = args[0];
Job = args[1];
Faculty = args[2];
Term = int.Parse(args[3]);
}
static public bool CheckFac(string fac, Student[] array)
{
bool temp = false;
foreach (Student s in array)
if (s.Faculty == fac)
temp = true;
return temp;
}
public int CompareTo(Student obj)
{
if (this.Term < obj.Term)
return 1;
if (this.Term > obj.Term)
return -1;
else
return 0;
}
public void Show()
{
Console.WriteLine("\nФИО: {0} \nДолжность: {1} \nНомер отдела: {2} \nСтаж: {3}", FIO, Job, Faculty, Term);
}
}
}