c#對(duì)list排序示例
更新時(shí)間:2014年01月06日 11:22:27 作者:
本文主要介紹了c#對(duì)List成員排序的示例,大家參考使用吧
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListSort
{
class Program
{
static void Main(string[] args)
{
List listCustomer = new List();
listCustomer.Add(new Customer { name = "客戶(hù)1", id = 0 });
listCustomer.Add(new Customer { name = "客戶(hù)2", id = 1 });
listCustomer.Add(new Customer { name = "客戶(hù)3", id = 5 });
listCustomer.Add(new Customer { name = "客戶(hù)4", id = 3 });
listCustomer.Add(new Customer { name = "客戶(hù)5", id = 4 });
listCustomer.Add(new Customer { name = "客戶(hù)6", id = 5 });
///升序
List listCustomer1 = listCustomer.OrderBy(s => s.id).ToList();
//降序
List listCustomer2 = listCustomer.OrderByDescending(s => s.id).ToList();
//Linq排序方式
List listCustomer3 = (from c in listCustomer
orderby c.id descending //ascending
select c).ToList();
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; }
}
}
您可能感興趣的文章:
- 關(guān)于C#泛型列表List<T>的基本用法總結(jié)
- C#讀取數(shù)據(jù)庫(kù)返回泛型集合詳解(DataSetToList)
- C#中數(shù)組Array,ArrayList,泛型List詳細(xì)對(duì)比
- C#控制臺(tái)基礎(chǔ) List泛型集合與對(duì)應(yīng)的數(shù)組相互轉(zhuǎn)換實(shí)現(xiàn)代碼
- C# List<T>的用法小結(jié)
- c#將list類(lèi)型轉(zhuǎn)換成DataTable方法示例
- C#中List和數(shù)組之間轉(zhuǎn)換的方法
- C#實(shí)現(xiàn)泛型List分組輸出元素的方法
相關(guān)文章
C#中的Task.Delay()和Thread.Sleep()區(qū)別(代碼案例)
Task.Delay(),async/await和CancellationTokenSource組合起來(lái)使用可以實(shí)現(xiàn)可控制的異步延遲。本文通過(guò)多種代碼案例給大家分析C#中的Task.Delay()和Thread.Sleep()知識(shí),感興趣的朋友一起看看吧2021-06-06c#模擬平拋運(yùn)動(dòng)動(dòng)畫(huà)的方法詳解
本篇文章是對(duì)使用c#模擬平拋運(yùn)動(dòng)動(dòng)畫(huà)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C# 無(wú)需COM組件創(chuàng)建快捷方式的實(shí)現(xiàn)代碼
做一個(gè)小程序, 需要?jiǎng)?chuàng)建快捷方式, 網(wǎng)上普遍的做法是引入 COM 組件, 雖然也挺方便的, 但引入之后, 程序就需要多帶一個(gè) dll 文件, 這樣, 想做成單文件便攜版就不行了2011-05-05利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫(kù)查詢(xún)
利用C#實(shí)現(xiàn)分布式數(shù)據(jù)庫(kù)查詢(xún)...2007-03-03