泛型IComparer<T>排序
class Program
{
static void Main(string[] args)
{
GetListTest();
} private static void GetListTest()
{
DBHelper dbHelper = DBHelper.GetInstance();
DataSet ds = dbHelper.GetSqlDataSet("SELECT name,age FROM tbl_test",null); List<Person> listPerson = new List<Person>();
for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
Person model = new Person();
model.Name = ds.Tables[].Rows[i]["name"].ToString();
model.Age = ds.Tables[].Rows[i]["age"].ToString();
listPerson.Add(model);
} //年龄排序
Console.WriteLine("----年龄排序----");
listPerson.Sort(new SortAge());
for (int i = ; i < listPerson.Count; i++)
{
Console.WriteLine(listPerson[i].Name + ":" + listPerson[i].Age);
}
Console.WriteLine("");
//姓名排序
Console.WriteLine("----姓名排序----");
listPerson.Sort(new SortName());
for (int i = ; i < listPerson.Count; i++)
{
Console.WriteLine(listPerson[i].Name + ":" + listPerson[i].Age);
}
} } /// <summary>
/// 排序实体
/// </summary>
class Person
{
private string name;
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
} private string age;
/// <summary>
/// 年龄
/// </summary>
public string Age
{
get { return age; }
set { age = value; }
}
} /// <summary>
/// 年龄排序
/// </summary>
class SortAge :IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Age.CompareTo(y.Age);
}
} /// <summary>
/// 姓名排序
/// </summary>
class SortName : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Name.CompareTo(y.Name);
}
}
泛型IComparer<T>排序的更多相关文章
- 泛型List<T>排序(利用反射)
在最近一个项目中,有需求要对页面中所有的gridview添加排序功能.由于gridview的数据源绑定的是一个集合类List,而不是DataTable,所以无法使用DataView排序功能.另外,不同 ...
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
wpf 导出Excel 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...
- List泛型集合对象排序
本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...
- c# 内部类使用接口IComparer实现排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- IComparer 指定排序。
public class NeEntityComparer : IComparer<NeEntity> { public int Compare(NeEntity x, NeEntity ...
- Java对象比较器对泛型List进行排序-Demo
针对形如:字段1 字段2 字段3 字段n 1 hello 26 7891 world 89 5562 what 55 4562 the 85 452 fuck 55 995 haha 98 455 以 ...
- C# 中 List.Sort运用(IComparer<T>)排序用法
/// <summary> /// 比较人物类实例大小,实现接口IComparer /// </summary> public class InternetProtocolCo ...
- 泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- [c#基础]泛型集合的自定义类型排序
引用 最近总有种感觉,自己复习的进度总被项目中的问题给耽搁了,项目中遇到的问题,不总结又不行,只能将复习基础方面的东西放后再放后.一直没研究过太深奥的东西,过去一年一直在基础上打转,写代码,反编译,不 ...
随机推荐
- ALTFP_CONVERT IP使用与仿真
ALTFP_CONVERT IP使用与仿真 近期项目要使用到整型数据转浮点型数据,将16位的整数转换为单精度浮点数(32bit).本打算自己写逻辑实现的,不过考虑到本身项目时间紧,能力也有限,就没 ...
- iOS完整App资源收集
前言 iOS开发学习者都希望得到实战训练,但是很多资料都是只有一小部分代码,并不能形成完成的App,笔者在此处收集了很多开源的完整的App,都有源代码哦! 本篇文章持续更新中,请持续关注.本篇所收集的 ...
- linux下echo命令详解(转)
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个 ...
- Python使用报错记录
问题1:pip 报错 C:\Users\Administrator>pip3 install pyreadline Fatal error in launcher: Unable to crea ...
- java String 深入理解
说出下面程序的输出 class StringEqualTest { public static void main(String[] args) { String s1 = "Program ...
- java实现贪吃蛇游戏
最简单的4个java类就可以实现贪吃蛇: main函数: package tcs; public class GreedSnake { public static void main(String[] ...
- Epoll,Poll,Select模型比较
http://blog.csdn.net/liangyuannao/article/details/7776057 先说Select: 1.Socket数量限制:该模式可操作的Socket数由FD_S ...
- 为测试框架model类自动生成xml结果集
问题:有大量类似于theProductId这样名字的字符串需要转换成the_product_id这种数据库column名的形式. 思路:见到(见)大写字母(缝)就插入(插)一个“_”字符(针)进去,最 ...
- kindeditor编辑器
一 简单使用方法 1. 把所有文件上传到程序所在目录下,例如:http://你的域名/editor/. 2. 在此目录下创建attached文件夹,并把权限改成777. 3. 要添加编辑器的地方加入以 ...
- python-dev无法安装
安装tensorflow之前需要先安装python-dev, 这个不能用pip install来安装,只能用sudo apt-get install来安装.安装后会发现还要先装另外两个东西,但是装这两 ...