Сортировка списка по алфавиту - C#
Формулировка задачи:
Добрый день,как нужно сделать сортировку списка по названию товара,с сортировкой нет проблем,проблема в доступе к полю с названием товара.Заранее благодарен.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class VideoSharp
{
static void Main()
{
Store store = new Store();
ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey();
Console.Write("Введите индекс товара : ");
int ind = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите название товара : ");
string name = Convert.ToString(Console.ReadLine());
Console.Write("Введите название магазина : ");
string storename = Convert.ToString(Console.ReadLine());
Console.Write("Введите цену товара : ");
double price = Convert.ToDouble(Console.ReadLine());
store.AddGoods(new Goods(ind, name, storename, price));
if (cki.Key == ConsoleKey.Escape) store.OutPut();
} while (cki.Key != ConsoleKey.Escape);
Console.ReadLine();
}
}
class Goods
{
int index;
string name;
string storename;
double price;
public int Index
{
get { return index; }
set { index = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string StoreName
{
get{return storename;}
set {storename = value;}
}
public double Price
{
get { return price; }
set { price = value; }
}
public Goods(int index, string name, string storename, double price)
{
this.index = index;
this.name = name;
this.price = price;
this.storename = storename;
}
}
class Store
{
ConsoleKeyInfo cki;
List<Goods> g = new List<Goods>();
public void AddGoods(Goods good)
{
g.Add(good);
}
void SortName()
{
//?????
}
void Sort()
{
int choice;
do
{
Console.Clear();
Console.WriteLine("1.Сортировка по названию");
Console.WriteLine("4.Выход");
choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
SortName();
break;
}
} while (choice != 4);
}
public void OutPut()
{
int choice;
do
{
Console.Clear();
Console.WriteLine("3.Сортировка");
Console.WriteLine("5.Выход");
choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 3:
Console.Clear();
Sort();
break;
case 5:
Environment.Exit(0);
break;
}
} while (choice != 5);
}
}Решение задачи: «Сортировка списка по алфавиту»
textual
Листинг программы
List<Goods> sortedList = list.OrderBy(x => x.Name).ToList();