惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
The Blog of Author Tim Ferriss
D
Docker
博客园 - 聂微东
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
量子位
宝玉的分享
宝玉的分享
博客园 - 司徒正美
The Cloudflare Blog
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC

博客园 - 朱小能

C#对Dictionary的按Value排序 linq to sql中的自动缓存(对象跟踪) javascript 调用webservice 的几种方法 修改域名映射IP地址 SQL 语句,类型转换 hyper-v 网络连接 VS2010 定位文件在solution中的位置 oracle连接字符串配置 spool的简单使用 open数据库Timeout expired 错误 文件的还原与备份 - 朱小能 - 博客园 ASCII码,对应e.KeyChar C# 调用计算器,日历 - 朱小能 - 博客园 Excel 如何由一个文本算术公式得到一个结果 Word、Excel中输入当前日期及时间的快捷键 c# ToString 格式化数字 - 朱小能 GridView绑定 获取access中表的相关信息 Filelog - 朱小能 - 博客园
List<T> 排序 - 朱小能 - 博客园
朱小能 · 2009-01-14 · via 博客园 - 朱小能

List<T> 排序

不说什么了,直接贴代码

  1 //Compare records  by selected column
  2    internal class ListComparer<T> : IComparer<T>
  3    {
  4        private string propertyName;
  5
  6        public ListComparer(string PropertyName)
  7        {
  8            propertyName = PropertyName;
  9        }

 10
 11        IComparer Members
106    }

107
108    //Sort list by sort field and  sortDirection.
109    public class SortList<T>
110    {
111        public static void Sort(List<T> list, string sortfield, bool isAscending)
112        {
113            ListComparer<T> listComparer = new ListComparer<T>(sortfield);
114            list.Sort(listComparer);
115            if (!isAscending) list.Reverse();
116        }

117    }

我们调用的时候,直接排序

 SortList<Model>.Sort(IListModel as List<Model>, "Name", true);