码迷,mamicode.com
首页 > 编程语言 > 详细

List排序的两种简便方式

时间:2015-05-06 21:10:50      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListSort
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> listCustomer = new List<Customer>();
            listCustomer.Add(new Customer { name = "客户1", id = 0 });
            listCustomer.Add(new Customer { name = "客户2", id = 1 });
            listCustomer.Add(new Customer { name = "客户3", id = 5 });
            listCustomer.Add(new Customer { name = "客户4", id = 3 });
            listCustomer.Add(new Customer { name = "客户5", id = 4 });
            listCustomer.Add(new Customer { name = "客户6", id = 5 });
            ///升序
            List<Customer> listCustomer1 = listCustomer.OrderBy(s => s.id).ToList<Customer>();
            //降序
            List<Customer> listCustomer2 = listCustomer.OrderByDescending(s => s.id).ToList<Customer>();
            //Linq排序方式
            List<Customer> listCustomer3 = (from c in listCustomer
                                            orderby c.id descending //ascending
                                            select c).ToList<Customer>();
            Console.WriteLine("List.OrderBy方法升序排序");
            foreach (Customer customer in listCustomer1)
            {
                Console.WriteLine(customer.name);
            }
            Console.WriteLine("List.OrderByDescending方法降序排序");
            foreach (Customer customer in listCustomer2)
            {
                Console.WriteLine(customer.name);
            }
            Console.WriteLine("Linq方法降序排序");
            foreach (Customer customer in listCustomer3)
            {
                Console.WriteLine(customer.name);
            }
            Console.ReadKey();
        }
    }
    class Customer
    {
        public int id { get; set; }
        public string name { get; set; }
    }
}

 

List排序的两种简便方式

标签:

原文地址:http://www.cnblogs.com/gisblogs/p/4483020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!