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. Java局部变量final

    局部变量和形参带final. 在一个线程A中开起另一个线程B,如果线程B要使用线程A的局部变量,那么A的局部变量需要定义成final.理由:局部变量是线程内部共享的,每一个线程内的不能访问其他线程的局 ...

  2. (转载)PERL 处理CSV

    #!/usr/bin/perl#use strict;open(FILE1,"C:/Perl/BX/BX-Users.csv");open(FILE2,"C:/Perl/ ...

  3. HDU 5787:K-wolf Number(数位DP)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5787 题意:要求相邻的K个位的数不能相同,在[L,R]区间有多少个这样的数. #inclu ...

  4. ACM题目————The Blocks Problem

    代码参考:http://www.hankcs.com/program/uva-q101-the-blocks-problem.html Description Background Many area ...

  5. HDU(1572),最短路,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...

  6. Codeforces Round #368 (Div. 2) A

    Description Small, but very brave, mouse Brain was not accepted to summer school of young villains. ...

  7. MVC api json 格式

    输出json var formatters = config.Formatters.Where(formatter => formatter.SupportedMediaTypes.Where( ...

  8. 【Web】简谈如何监听浏览器的关闭

    > 参考的优秀文章 beforeunload实现关闭离开的提示 想起以前做的一个小系统,一个企业内部小型的测试系统,让考生在给定时间内完成考试,如果考生中退出,那么下次进来可以利用剩余的考试时间 ...

  9. Monkey类、People类和主类 E。

    package jicheng; public class Monkey { private String s; public String getS() { return s; } public v ...

  10. JAVA题 矩形

    package com.lo; public class juxing { private double chang; private double kuan; public double getCh ...