Вывести список студентов в порядке убывания - C#
Формулировка задачи:
Добрый вечер. Ребята, помогите отсортировать считанный список.
В файл нужно вывести фамилию и имя студента в порядке убывания их среднего балла.
Очень нужно. Спасибо за внимание)
Пример файла IN:
4 Ivanov Vasiliy 5 3 4 Petrov Sergey 4 3 5 Konstantinov Nikolay 5 5 5 Kuznetsov Ivan 5 4 4
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace New_3
{
class Student
{
public string surname;
public string name;
public byte phys;
public byte mat;
public byte info;
public Student()
{
surname = null;
name = null;
phys = 0;
mat = 0;
info = 0;
}
public Student(string surname, string name, byte mat, byte phys, byte info)
{
this.surname = surname;
this.name = name;
this.mat = mat;
this.phys = phys;
this.info = info;
}
public string Output()
{
return string.Format("{0} {1} {3} {4}", surname, name, mat, phys, info);
}
}
class Program
{
static void Main(string[] args)
{
StreamReader input = new StreamReader("C:/IN.txt");
byte n = byte.Parse(input.ReadLine());
Console.WriteLine(n);
Student[] students = new Student[n];
string buf = null;
string[] bufsplit;
for (int i = 0; i < n; i++)
{
buf = input.ReadLine();
bufsplit = buf.Split(' ');
students[i] = new Student(bufsplit[0], bufsplit[1], byte.Parse(bufsplit[2]), byte.Parse(bufsplit[3]), byte.Parse(bufsplit[4]));
Console.WriteLine("{0} {1}", i + 1, students[i].Output());
}
Console.WriteLine("Файл считан");
StreamWriter output = new StreamWriter("C:/OUT.txt");
//блок обработки считанных данных
output.Close();
input.Close();
}
}
}Решение задачи: «Вывести список студентов в порядке убывания»
textual
Листинг программы
using System.IO;
using System.Linq;
class Class13
{
static void Main()
{
var t = File.ReadLines("in.txt").Skip(1).Select(n =>
{
var strings = n.Split();
int phys = int.Parse(strings[2]),
mat = int.Parse(strings[3]),
info = int.Parse(strings[4]);
return
new
{
surname = strings[0],
name = strings[1],
average = ((double)phys + mat + info) / 3
};
}).OrderByDescending(n => n.average).Select(n => n.surname + " " + n.name);
File.WriteAllLines("result.txt", t);
}
}