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

推荐订阅源

WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
B
Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Jina AI
Jina AI
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
L
LangChain Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
F
Fortinet All Blogs
H
Help Net Security
B
Blog RSS Feed
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题

博客园 - 朱小能

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);