为collection增加排序功能

简介:


 

注意到 CollectionBase.InnerList属性,这是一个protected 属性,得到一个包含了CollectionBase中的元素的ArrayList。

所以可以调用ArrayList.Sort()来实现排序。

 

using System;

 

public class Person {

      public Person(string name, int age) {

            this.Name = name;

            this.Age = age;

      }

      public string Name;

      public int Age;

}

 

public class People : System.Collections.CollectionBase {

      public int Add(Person p) {

            return List.Add(p);

      }

 

      public void SortByName() {

            System.Collections.IComparer sorter = new NameSortHelper();

            InnerList.Sort(sorter);

      }

      public void SortByAge() {

            System.Collections.IComparer sorter = new AgeSortHelper();

            InnerList.Sort(sorter);

      }

 

      private class NameSortHelper : System.Collections.IComparer {

            public int Compare(object x, object y) {

                  Person p1 = (Person) x;

                  Person p2 = (Person) y;

                  return p1.Name.CompareTo(p2.Name);

            }

      }

 

      private class AgeSortHelper : System.Collections.IComparer {

            public int Compare(object x, object y) {

                  Person p1 = (Person) x;

                  Person p2 = (Person) y;

                  if(p1.Age > p2.Age)

                        return 1;

                  if(p1.Age < p2.Age)

                        return -1;

                  return 0;

            }

      }

}

 

 

class App {

      [STAThread]

      static void Main(string[] args) {

            People people = new People();

            people.Add(new Person("John" , 30));

            people.Add(new Person("Bob" , 20));

            people.Add(new Person("Zeke" , 18));

           

            //people.SortByAge();

            people.SortByName();

 

            foreach(Person p in people) {

                  Console.WriteLine("{0} , {1}", p.Name, p.Age);

            }

      }

}

 

 

http://unboxedsolutions.com/sean/archive/2004/08/10/292.aspx

作者: 峻祁连
邮箱:junqilian@163.com 
出处: http://junqilian.cnblogs.com 
转载请保留此信息。



本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/archive/2008/08/19/1271203.html ,如需转载请自行联系原作者
相关文章
|
4月前
|
索引
单列集合顶层接口Collection
单列集合顶层接口Collection
26 1
List集合如何分页
List集合如何分页(List集合转Page分页)
109 0
|
10月前
|
C#
c#集合去重&排序常用方法
list和数组转Hashset跟SortedSet进行排序和去重,以及当Hashset和SortedSet存放的是类时如何进行自定义的排序和去重
80 2
|
10月前
|
存储 监控 Java
Set集合去重(详细篇)
Set集合去重(详细篇)
155 0
|
存储 Java 程序员
Java集合List介绍和去重方案
Java集合List介绍和去重方案
89 0
|
数据库
List中对象去重和List 根据对象的属性去重
有这么一个需求,需要将从数据库查出来的数据进行去重。
87 0
|
存储 NoSQL Redis
Sorted Set源码阅读,有序集合为何能同时支持点查询和范围查询
Sorted Set源码阅读,有序集合为何能同时支持点查询和范围查询
34 0
|
Shell
List集合如何去重?
将 l i s t 集 合 放 入 L i n k e d H a s h S e t 集 合 中 , 然 后 再 重 新 添 加 到 l i s t 集 合 中 。 \color{#FF0000}{将list集合放入LinkedHashSet集合中,然后再重新添加到list集合中。}将list集合放入LinkedHashSet集合中,然后再重新添加到list集合中。
96 0
List集合如何去重?
|
Java 程序员
List 去重的 6 种方法,这种方法最完美!
List 去重的 6 种方法,这种方法最完美!
291 0
List 去重的 6 种方法,这种方法最完美!
List中根据某个实体的属性去重或者排序
List中根据某个实体的属性去重或者排序
216 0