Как отсортировать массив по по полю ? - C#
Формулировка задачи:
C# как отсортировать массив по по полю Vp?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Project10
{
struct Bus
{
public int N;
public string Pn;
public double Vo;
public double Vp;
public Bus(int n, string pn, double vo, double vp)
{
this.N = n;
this.Pn = pn;
this.Vo = vo;
this.Vp = vp;
}
public void Show()
{
Console.WriteLine("Найден рейс по вашему запросу ");
Console.WriteLine("№ рейса " + this.N);
Console.WriteLine("Пункт назначения: " + this.Pn);
Console.WriteLine("Время отправления: " + this.Vo);
Console.WriteLine("Время прибытия : " + this.Vp);
}
public void Write()
{
string fileName = "bus.txt";
FileStream aFile = new FileStream(fileName, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
aFile.Seek(0, SeekOrigin.End);
sw.WriteLine("№ рейса " + this.N);
sw.WriteLine("Пункт назначения: " + this.Pn);
sw.WriteLine("Время отправления: " + this.Vo);
sw.WriteLine("Время прибытия : " + this.Vp);
sw.WriteLine();
sw.Close();
Console.WriteLine();
Console.WriteLine("Данные записаны в файл");
}
}
class PR10
{
public static void Main()
{
string pnb;
double qvp = 8.15;
int k = 2;
Bus[] bus = new Bus[k];
bus[0] = new Bus(121, "Pris", 7.20, 8.15);
bus[1] = new Bus(122, "Roma", 14.00, 14.50);
Console.Write("Введите пункт прибытия : ");
pnb = Console.ReadLine();
Console.Write("Время: ");
qvp = Convert.ToDouble(Console.ReadLine());
Console.Read();
for (int i = 0; i < k; i++)
if (bus[i].Pn == pnb && bus[i].Vp < qvp)
{
bus[i].Show();
bus[i].Write();
}
Console.Read();
Console.Read();
}
}
}Решение задачи: «Как отсортировать массив по по полю ?»
textual
Листинг программы
Array.Sort(bus, (x, y) => x.Vp.CompareTo(y.Vp));