国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

通過寫一個Demo展示C#中多種常用的集合排序方法

2019-11-17 02:21:31
字體:
供稿:網(wǎng)友

通過寫一個Demo展示C#中多種常用的集合排序方法

不多說,程序很簡單,就是將集合中的數(shù)據(jù)進行排序,但使用到的知識點還是比較多的,大牛勿噴,謹獻給初學者!直接上程序吧!

namespace Demo{    /// <summary>    /// Demo:使用不同排序方法對元素進行排序    /// </summary>    class PRogram    {        private static void Main(string[] args)        {            ArrayList arrayList = Product.GetArrayList();            List<Product> list = Product.GetList();            //1、使用繼承IComparer接口的函數(shù)來進行排序            arrayList.Sort(new ProductCompare());            foreach (Product product in arrayList)            {                Console.WriteLine(product.ToString());            }            Console.WriteLine("---------------------------");            //2、使用繼承IComparer<T>接口的函數(shù)來進行排序            list.Sort(new ProductCompareT());            foreach (Product product in list)            {                Console.WriteLine(product.ToString());            }            Console.WriteLine("---------------------------");                        //3、使用委托來進行排序            list.Sort(delegate(Product x, Product y)            {                return x.Price.CompareTo(y.Price);            });            foreach (Product product in list)            {                Console.WriteLine(product.ToString());            }            //4、使用Lambda表達式來進行排序;            list.Sort((x, y) => x.Price.CompareTo(y.Price));            foreach (Product product in list)            {                Console.WriteLine(product.ToString());            }            //5、使用擴展方法來進行排序            foreach (Product product in list.OrderBy(p=>p.Price))            {                Console.WriteLine(product.ToString());            }            Console.ReadKey();        }    }    public class Product    {        public string Name { get; set; }        public decimal Price { get; set; }        public static ArrayList GetArrayList()        {            return new ArrayList()            {                new Product {Name = "WindowsPhone", Price = 10m},                new Product {Name = "Apple", Price = 20m},                new Product {Name = "Android", Price = 5m}            };        }        public static List<Product> GetList()        {            return new List<Product>()            {                new Product {Name = "WindowsPhone", Price = 10m},                new Product {Name = "Apple", Price = 20m},                new Product {Name = "Android", Price = 5m}            };        }        public override string ToString()        {            return String.Format("{0}--{1}", Name, Price);        }    }    /// <summary>    /// 使用IComparer對ArrayList進行排序    /// 顯示實現(xiàn)Compare接口,常用ArrayList類型的集合來調(diào)用    /// </summary>    public class ProductCompare : IComparer    {        public int Compare(object x, object y)        {            Product first = x as Product;            Product second = y as Product;            if (first != null && second !=null)            {                return first.Price.CompareTo(second.Price);            }            else            {                return -1;            }        }    }    /// <summary>    /// 使用IComparer<Product>進行排序    /// 顯式實現(xiàn)Compare接口,常用List<T>類型的集合來調(diào)用    /// </summary>    public class ProductCompareT : IComparer<Product>    {        public int Compare(Product x, Product y)        {            Product first = x as Product;            Product second = y as Product;            if (first != null && second != null)            {                return first.Price.CompareTo(second.Price);            }            else            {                return -1;            }        }    }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洮南市| 那坡县| 宁强县| 阳原县| 寻乌县| 策勒县| 长乐市| 平利县| 兴海县| 丰城市| 威远县| 苏尼特左旗| 砚山县| 临沂市| 古蔺县| 南通市| 潮安县| 定日县| 淳安县| 陈巴尔虎旗| 玛沁县| 揭东县| 宁河县| 汝州市| 集贤县| 娱乐| 平利县| 义马市| 伊宁市| 岱山县| 崇左市| 宣威市| 科技| 赤峰市| 大洼县| 金寨县| 阳城县| 玉林市| 甘德县| 井陉县| 河北区|