C#List能按照List中的物件某個屬性排序
阿新 • • 發佈:2019-01-03
public class Book
{
public Book(string inname, string inauthor, string inpublisher, int inprice)
{
this.Name = inname;
this.Author = inauthor;
this.Publisher = inpublisher;
this.Price = inprice;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
private string publisher;
public string Publisher
{
get { return publisher; }
set { publisher = value; }
}
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
}
//比較器類
public class Icp:IComparer<Book>
{
//按書名排序
public int Compare(Book x, Book y)
{
return x.Name.CompareTo(y.Name);
}
}
public class IcpPrice : IComparer<Book>
{
//按價格排序
public int Compare(Book x, Book y)
{
return x.Price.CompareTo(y.Price);
}
}
//main方法
static void Main(string[] args)
{
List<Book> list = new List<Book>();
Book b1, b2, b3, b4;
b1 = new Book("《絕代雙驕》", "古龍", "中國長安出版社", 50);
b2 = new Book("《鹿鼎記》", "金庸", "人民文學出版社", 120);
b3 = new Book("《三國演義》", "羅貫中", "中州古籍出版社", 200);
b4 = new Book("《西遊記》", "吳承恩", "晨光出版社", 80);
list.Add(b1);
list.Add(b2);
list.Add(b3);
list.Add(b4);
//按書名升序
Console.WriteLine("/////////////按書名升序/////////////");
list.Sort(new Icp());
foreach(Book book in list)
{
Console.WriteLine(book.Name+book.Author+book.Publisher+book.Price);
}
Console.WriteLine("////////////////////////////////////");
//按價格降序序
Console.WriteLine("/////////////按書名升序/////////////");
list.Sort(new IcpPrice());
foreach (Book book in list)
{
Console.WriteLine(book.Name + book.Author + book.Publisher + book.Price);
}
Console.WriteLine("////////////////////////////////////");
Console.Read();
}能CdsaC#List<>能按照List中的物件某個屬性排序dgbnhbfjhsa #List<>能按照List中的物件某個屬性排序按照List中的物件某個屬性排序嗎
{
public Book(string inname, string inauthor, string inpublisher, int inprice)
{
this.Name = inname;
this.Author = inauthor;
this.Publisher = inpublisher;
this.Price = inprice;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
private string publisher;
public string Publisher
{
get { return publisher; }
set { publisher = value; }
}
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
}
//比較器類
public class Icp:IComparer<Book>
{
//按書名排序
public int Compare(Book x, Book y)
{
return x.Name.CompareTo(y.Name);
}
}
public class IcpPrice : IComparer<Book>
{
//按價格排序
public int Compare(Book x, Book y)
{
return x.Price.CompareTo(y.Price);
}
}
//main方法
static void Main(string[] args)
{
List<Book> list = new List<Book>();
Book b1, b2, b3, b4;
b1 = new Book("《絕代雙驕》", "古龍", "中國長安出版社", 50);
b2 = new Book("《鹿鼎記》", "金庸", "人民文學出版社", 120);
b3 = new Book("《三國演義》", "羅貫中", "中州古籍出版社", 200);
b4 = new Book("《西遊記》", "吳承恩", "晨光出版社", 80);
list.Add(b1);
list.Add(b2);
list.Add(b3);
list.Add(b4);
//按書名升序
Console.WriteLine("/////////////按書名升序/////////////");
list.Sort(new Icp());
foreach(Book book in list)
{
Console.WriteLine(book.Name+book.Author+book.Publisher+book.Price);
}
Console.WriteLine("////////////////////////////////////");
//按價格降序序
Console.WriteLine("/////////////按書名升序/////////////");
list.Sort(new IcpPrice());
foreach (Book book in list)
{
Console.WriteLine(book.Name + book.Author + book.Publisher + book.Price);
}
Console.WriteLine("////////////////////////////////////");
Console.Read();
}能CdsaC#List<>能按照List中的物件某個屬性排序dgbnhbfjhsa #List<>能按照List中的物件某個屬性排序按照List中的物件某個屬性排序嗎