泛型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#基础]泛型集合的自定义类型排序
引用 最近总有种感觉,自己复习的进度总被项目中的问题给耽搁了,项目中遇到的问题,不总结又不行,只能将复习基础方面的东西放后再放后.一直没研究过太深奥的东西,过去一年一直在基础上打转,写代码,反编译,不 ...
随机推荐
- List对象排序的通用方法
转自 @author chenchuang import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Me ...
- Hibernate,JPA注解@SecondaryTable
使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所 ...
- ACM题目————Anagram
Description You are to write a program that has to generate all possible words from a given set of l ...
- oracle驱动地址
D:\app\LHComputer\product\12.1.0\dbhome_1\ODP.NET\bin\2.x
- 单片机C语言开发学习笔记---动态的数码管
在郭天祥的那本书中,有一个通过按键控制数码管的例子,在运行这个例子的时候,我发现当按键按下的时候,第一位数码管会熄掉,这是为什么呢? 后来在网上找到了原因,当我按下按键不松开的时候,接下来要运行的代码 ...
- norm函数
如果A为向量 norm(A,p) 返回向量A的p范数. norm(A) 返回向量A的2范数,即等价于norm(A,2).
- 2016年10月24日 星期一 --出埃及记 Exodus 19:8
2016年10月24日 星期一 --出埃及记 Exodus 19:8 The people all responded together, "We will do everything th ...
- Redis的WEB界面管理工具phpRedisAdmin
下载地址:http://down.admin5.com/php/75024.html 官方网址:https://github.com/ErikDubbelboer/phpRedisAdmin
- LAMMP架构的企业级应用
LAMMP架构的企业级应用 ========================================= LAMMP是什么 LAMMP的实现 LAMMP适用的生产环境 ============= ...
- spark1.4
spark1.4 Windows local调试环境搭建总结 1.scala版本 scala-2.10.4 官方推荐 scala-2.11.7[不推荐,非sbt项目.需要后加载] 2.spark版本 ...