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>排序的更多相关文章

  1. 泛型List<T>排序(利用反射)

    在最近一个项目中,有需求要对页面中所有的gridview添加排序功能.由于gridview的数据源绑定的是一个集合类List,而不是DataTable,所以无法使用DataView排序功能.另外,不同 ...

  2. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  3. List泛型集合对象排序

    本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...

  4. c# 内部类使用接口IComparer实现排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. IComparer 指定排序。

    public class NeEntityComparer : IComparer<NeEntity> { public int Compare(NeEntity x, NeEntity ...

  6. 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 以 ...

  7. C# 中 List.Sort运用(IComparer<T>)排序用法

    /// <summary> /// 比较人物类实例大小,实现接口IComparer /// </summary> public class InternetProtocolCo ...

  8. 泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. [c#基础]泛型集合的自定义类型排序

    引用 最近总有种感觉,自己复习的进度总被项目中的问题给耽搁了,项目中遇到的问题,不总结又不行,只能将复习基础方面的东西放后再放后.一直没研究过太深奥的东西,过去一年一直在基础上打转,写代码,反编译,不 ...

随机推荐

  1. javaWeb 在jsp中 使用自定义标签输出访问者IP

    1.java类,使用简单标签,jsp2.0规范, 继承 SimpleTagSupport public class ViewIpSimpleTag extends SimpleTagSupport { ...

  2. 深入研究java.lang.Runtime类【转】

    转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...

  3. oracle 数据泵 每天自动备份

    @echo off echo 删除10天前的备分文件和日志 forfiles /p "e:\app\back" /m *.dmp /d -5 /c "cmd /c del ...

  4. java中枚举类的使用详解

    /* * 通过JDK5提供的枚举来做枚举类 */ public enum Direction2 { FRONT("前"), BEHIND("后"), LEFT( ...

  5. weblogic的jar包冲突

    异常: Error creating bean with name 'sessionFactoryWrite' defined in class path resource [applicationC ...

  6. 【PHP设计模式 03_JianDanGongChang.php】 简单工厂

    <?php /** * [简单工厂] * 之前 02.php 面向接口开发,客户端还是知道了服务器端的所有类. * 现在想让客户端只知道一个类,就用工厂. */ header("Con ...

  7. mongo 学习教程(全)

    看的是爱酷学习网的视频:http://www.icoolxue.com/album/show/98 01 安装 1.先建mongoDB-data文件夹存数据 2.安装DB 3.设置环境变量:把bin目 ...

  8. PHP学习当中遗漏的知识点

    一, 当双引号中包含变量时,变量会与双引号中的内容连接在一起: 当单引号中包含变量时,变量会被当做字符串输出. <?php $love = "I love you!"; $s ...

  9. PostgreSQL中使用外部表

    1. 安装file_fdw 需要先安装file_fdw,一般是进到PostgreSQL的源码包中的contrib/file_fdw目录下,执行: make make install 然后进入数据库中, ...

  10. java 面试每日一题6

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5 ...